我刚刚开始使用 Python/Tkinter 编写一个小型 Pymol 插件。在这里,我尝试有一个切换按钮并在单击它时报告其状态。该按钮会向上和向下移动,但是 toggleAVA
永远不会被调用。有什么想法吗?
from Tkinter import *
import tkMessageBox
class AVAGnome:
def __init__(self, master):
# create frames
self.F1 = Frame(rootGnome, padx=5, pady=5, bg='red')
# checkbuttons
self.AVAselected = IntVar()
self.AVAselected.trace("w", self.toggleAVA)
self.AVAbutton = Checkbutton(self.F1, text='AVA', indicatoron=0, variable=self.AVAselected)
# start layout procedure
self.layout()
def layout(self):
self.F1.pack(side=TOP, fill=BOTH, anchor=NW)
#entry and buttons
self.AVAbutton.pack(side=LEFT)
def toggleAVA(self, *args):
if (self.AVAselected.get()):
avastatus = "selected"
else:
avastatus = "unselected"
tkMessageBox.showinfo("AVA status", avastatus)
def __init__(self):
open_GnomeUI()
def open_GnomeUI():
# initialize window
global rootGnome
rootGnome = Tk()
rootGnome.title('AVAGnome')
global gnomeUI
gnomeUI = AVAGnome(rootGnome)
最佳答案
我使用 Pymol
测试了您的代码。
问题是因为您使用 Tk()
创建窗口。您必须使用 Toplevel()
,然后它才能与 trace()
或 command=
一起正常工作。
Pymol
是用 tkinter
创建的,它只能有一个用 Tk()
创建的窗口 - 它是程序中的主窗口。所有其他窗口都必须使用 Toplevel()
创建。
关于python - IntVar().trace() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41819128/