当按钮命令没有回调函数时,可以通过以下方式解决:
from tkinter import *
def button_clicked():
print("按钮被点击了!")
root = Tk()
button = Button(root, text="点击我", command=lambda: button_clicked())
button.pack()
root.mainloop()
from tkinter import *
def button_clicked():
print("按钮被点击了!")
def callback():
button_clicked()
root = Tk()
button = Button(root, text="点击我", command=callback)
button.pack()
root.mainloop()
from tkinter import *
class App:
def __init__(self, master):
button = Button(master, text="点击我", command=self.button_clicked)
button.pack()
def button_clicked(self):
print("按钮被点击了!")
root = Tk()
app = App(root)
root.mainloop()
上一篇:按钮命令和方向数组的问题
下一篇:按钮命令没有执行函数。