简体   繁体   English

如何分配 function?

[英]how do I assign a function?

I'm new to python, and I'm trying to work with “tkinter”.我是 python 的新手,我正在尝试使用“tkinter”。

The bottom line is that I need to assign a folder deletion function to the button底线是我需要将文件夹删除 function 分配给按钮

my code looks like this我的代码看起来像这样

from tkinter.ttk import LabeledScale 

import shutil

import pathlib 

master = tk.Tk()

lable = tk.Label(text ="delete this?")

lable.pack()

path = "C:\\Users\\kolba\\Desktop\\pythonpool"

def buttonClick():
    shutil.rmtree(path)

button = tk.Button(master, text ="yes!!!", ) #what to put after the comma?
 
button.pack()

master.mainloop()

how do I make the button work?如何使按钮工作?

You are looking for command你正在寻找命令

Function or method to be called when the button is clicked. Function 或单击按钮时要调用的方法。

In this case在这种情况下

def buttonClick():
    shutil.rmtree(path)

button = tk.Button(master, text="yes!!!", command=buttonClick)

You should pass the function after the comma:您应该在逗号后传递 function:

button = tk.Button(master, text ="yes!!!", command = buttonClick)

You can read more about tk.Button here您可以在此处阅读有关tk.Button的更多信息

you could add a font and you should put a command otherwise that button will be for nothing.你可以添加一个字体,你应该输入一个命令,否则该按钮将一事无成。 something like this:像这样的东西:

from tkinter.ttk import LabeledScale 

import shutil

import pathlib 

master = tk.Tk()

lable = tk.Label(text ="delete this?")

lable.pack()

path = "C:\\Users\\kolba\\Desktop\\pythonpool"

def buttonClick():
    shutil.rmtree(path)

button = tk.Button(master, text ="yes!!!",command=buttonClick ) 
 
button.pack()

master.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM