簡體   English   中英

如何分配 function?

[英]how do I assign a function?

我是 python 的新手,我正在嘗試使用“tkinter”。

底線是我需要將文件夾刪除 function 分配給按鈕

我的代碼看起來像這樣

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()

如何使按鈕工作?

你正在尋找命令

Function 或單擊按鈕時要調用的方法。

在這種情況下

def buttonClick():
    shutil.rmtree(path)

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

您應該在逗號后傳遞 function:

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

您可以在此處閱讀有關tk.Button的更多信息

你可以添加一個字體,你應該輸入一個命令,否則該按鈕將一事無成。 像這樣的東西:

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