如何在不单击鼠标的情况下执行python tkinter按钮

时间:2014-11-10 11:14:55

标签: python tkinter

我创建了一个按钮小部件:

button.configure(command=some command)

通过单击按钮,它可以在运行时运行(当它在mainloop指令中时)。现在我想在不点击鼠标的情况下执行按钮。是否有任何指令或过程来执行按钮小部件而不点击?

1 个答案:

答案 0 :(得分:3)

尝试invoke()窗口小部件的Button方法,例如(docs):

from Tkinter import *
import tkMessageBox

t = Tk()

def button1_click():
    tkMessageBox.showinfo("Message", "Bang!")

def button2_click():
    button1.invoke()

button1 = Button(t, text="Button 1", command=button1_click)
button1.pack()

button2 = Button(t, text="Button 2", command=button2_click)
button2.pack()

mainloop()

样式按钮:

import tkFont
font = tkFont.Font(size=32, weight="bold")

button['bg'] = "red"
button['fg'] = "white"
button['font'] = font