【发布时间】:2017-06-08 17:12:41
【问题描述】:
我刚刚开始使用 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)
【问题讨论】:
-
Checkbutton有选项command=所以也许使用command=self.toggleAVA而不是trace() -
是的,但这只是一个非常基本的示例。如果
self.AVAselected通过其他方式,我可能希望更改状态。无论如何,我试图理解为什么应该工作的功能不起作用 -
另外,我确实尝试过(使用
command),无论按钮是向上还是向下,self.AVAselected的状态都会报告为unselected。 -
我试过你的代码,它作为独立程序工作。如果程序使用两个
Tk()和两个mainloop(),我在其他程序中看到了类似的变量问题。也许您必须简单地使用Toplevel()而不是Tk()来创建窗口。