Python 为什么";“退出”;按钮在“关闭”后停止工作;否";按钮被点击了吗?
我有一个“退出”按钮,它创建了一个顶层窗口,询问用户是否确定要退出。如果单击“否”按钮,窗口将被破坏,但“退出”按钮不再工作。呃,“退出者”虚拟事件击键(CTRL-Q、Q和CTRL-C)也会停止工作。为什么呢Python 为什么";“退出”;按钮在“关闭”后停止工作;否";按钮被点击了吗?,python,python-2.7,tkinter,destroy,toplevel,Python,Python 2.7,Tkinter,Destroy,Toplevel,我有一个“退出”按钮,它创建了一个顶层窗口,询问用户是否确定要退出。如果单击“否”按钮,窗口将被破坏,但“退出”按钮不再工作。呃,“退出者”虚拟事件击键(CTRL-Q、Q和CTRL-C)也会停止工作。为什么呢 from Tkinter import * class ButtonTestClass(Frame): def __init__(self): self.root=Tk() self.root.title("No Button Tester!")
from Tkinter import *
class ButtonTestClass(Frame):
def __init__(self):
self.root=Tk()
self.root.title("No Button Tester!")
self.root.option_add('*font', ('arial', 14, 'bold'))
self.frame=Frame(self.root, height=120, width=600, \
borderwidth=2, relief=GROOVE, background='steelblue'
)
self.frame.pack(fill=X, expand=False)
self.frame.pack_propagate(False)
self.label = Label(self.frame, \
text='Why does the "Quit" button stop working\nafter the "No" button is clicked?', bg='steelblue')
self.label.pack(padx=25, pady=15, side=TOP)
self.root.event_add('<<ev_quit>>', '<Control-c>', '<Control-q>', '<q>')
self.root.bind('<<ev_quit>>', lambda e: self.bye_bye())
self.byeWindow = None
print "In constructor self.byeWindow - value: '%r' type: '%r'" % (self.byeWindow, type(self.byeWindow)) # <<< DEBUG >>>
def createQuitButton(self):
pbx=250; pby=70; pbw=75; pbh=25; pbp=10
self.quitBtn=Button(self.frame, text='Quit', activebackground='steelblue3', font='arial 10 bold', bg='steelblue', command=lambda: self.bye_bye())
self.quitBtn.place(x=pbx, y=pby, width=pbw, height=pbh)
pbx += pbw + pbp
def dontQuit(self, tlref):
self.byeWindow = None # <<<--- There was a 'misspelling' here; grrrr!
tlref.destroy()
print "In dontQuit(). self.byeWindow - value: '%r' type: '%r'" % (self.byeWindow, type(self.byeWindow)) # <<< DEBUG >>>
def bye_bye(self):
if self.byeWindow is not None:
return
self.byeWindow=Toplevel()
print "In bye_bye(), self.byeWindow - value: '%r' type: '%r'" % (self.byeWindow, type(self.byeWindow)) # <<< DEBUG >>>
self.byeWindow.title("Really quit?")
self.byeWindow.config(bg='steelblue', height=40, width=80)
sureMsgLabel=Label(self.byeWindow, text="Are you sure you want to quit?", font='arial 11 bold', bg='steelblue')
sureMsgLabel.pack(side=TOP, padx=10, pady=15)
yesButton=Button(self.byeWindow, text=" Yes. ", font='arial 10 bold', bg='steelblue', activebackground='steelblue3', command=lambda: quit())
yesButton.pack(side=LEFT, anchor=N, padx=40, pady=20)
yesButton.bind('<Key-Return>', lambda: yesButton.invoke())
noButton = Button(self.byeWindow, text=" No. ", activebackground='steelblue3', font='arial 10 bold', bg='steelblue', command=lambda: self.dontQuit(self.byeWindow))
noButton.focus_force()
noButton.pack(side=RIGHT, anchor=N, padx=40, pady=20)
noButton.bind('<Key-Return>', lambda: noButton.invoke())
bT = ButtonTestClass()
bT.createQuitButton()
if __name__ == '__main__':
bT.root.mainloop()
从Tkinter导入*
类按钮测试类(框架):
定义初始化(自):
self.root=Tk()
self.root.title(“无按钮测试器!”)
self.root.option_add('*font',('arial',14',bold'))
self.frame=frame(self.root,高度=120,宽度=600\
边框宽度=2,浮雕=凹槽,背景='steelblue'
)
self.frame.pack(fill=X,expand=False)
self.frame.pack_传播(False)
self.label=标签(self.frame\
text='为什么单击“否”按钮后“退出”按钮停止工作?',bg='steelblue')
self.label.pack(padx=25,pady=15,side=TOP)
self.root.event_add(“”,“”,“”,“”)
self.root.bind(“”,lambda e:self.bye_bye())
self.byeWindow=无
在构造函数self.byeWindow中打印“值:'%r'类型:'%r'”(self.byeWindow,类型(self.byeWindow))#>
def CreateQuit按钮(自身):
pbx=250;pby=70;pbw=75;pbh=25;pbp=10
self.quitBtn=按钮(self.frame,text='Quit',activebackground='steelblue3',font='arial 10 bold',bg='steelblue',command=lambda:self.bye_bye())
自平衡位置(x=pbx,y=pby,宽度=pbw,高度=pbh)
pbx+=pbw+pbp
def dontQuit(自身、tlref):
self.byeWindow=None#结果表明,在最终分析中,我的问题是一个实例变量拼写错误:“byWindow”而不是“byeWindow”,这将阻塞self.bye_bye()函数中的if语句。纠正拼写纠正了这个问题。非常感谢和亲切问候布赖恩·奥克利,感谢他提出的富有洞察力的建议和睿智的建议。这是完美运行的程序
from Tkinter import *
class ButtonTestClass(Frame):
def __init__(self):
self.root=Tk()
self.root.title("Button Tester!")
self.root.option_add('*font', ('arial', 14, 'bold'))
self.frame=Frame(self.root, height=120, width=600, \
borderwidth=2, relief=GROOVE, background='steelblue'
)
self.frame.pack(fill=X, expand=False)
self.frame.pack_propagate(False)
self.label = Label(self.frame, \
text='Wanna see a "Quit" button working flawlessly?', bg='steelblue')
self.label.pack(padx=25, pady=15, side=TOP)
self.root.event_add('<<ev_quit>>', '<Control-c>', '<Control-q>', '<q>')
self.root.bind('<<ev_quit>>', lambda e: self.bye_bye())
self.byeWindow = None
def createQuitButton(self):
pbx=250
pby=70
pbw=75
pbh=25
pbp=10
self.quitBtn=Button(
self.frame,
text='Quit',
activebackground='steelblue3',
font='arial 10 bold',
bg='steelblue',
command=self.bye_bye
)
self.quitBtn.place(
x=pbx,
y=pby,
width=pbw,
height=pbh
)
pbx += pbw + pbp
def dontQuit(self, tlref):
tlref.destroy()
self.byeWindow = None
def bye_bye(self):
if self.byeWindow is not None:
return
self.byeWindow=Toplevel()
self.byeWindow.title("Really quit?")
self.byeWindow.config(bg='steelblue', height=40, width=80)
sureMsgLabel=Label(self.byeWindow,
text="Are you sure you want to quit?",
font='arial 11 bold',
bg='steelblue'
)
sureMsgLabel.pack(side=TOP, padx=10, pady=15)
yesButton=Button(self.byeWindow,
text=" Yes. ",
font='arial 10 bold',
bg='steelblue',
activebackground='steelblue3',
command=lambda: quit()
)
yesButton.pack(side=LEFT, anchor=N, padx=40, pady=20)
yesButton.bind('<Key-Return>', lambda: yesButton.invoke())
noButton = Button(self.byeWindow,
text=" No. ",
activebackground='steelblue3',
font='arial 10 bold',
bg='steelblue',
command=lambda: self.dontQuit(self.byeWindow)
)
noButton.focus_force()
noButton.pack(side=RIGHT, anchor=N, padx=40, pady=20)
noButton.bind('<Key-Return>', lambda: noButton.invoke())
bT = ButtonTestClass()
bT.createQuitButton()
if __name__ == '__main__':
bT.root.mainloop()
从Tkinter导入*
类按钮测试类(框架):
定义初始化(自):
self.root=Tk()
self.root.title(“按钮测试器!”)
self.root.option_add('*font',('arial',14',bold'))
self.frame=frame(self.root,高度=120,宽度=600\
边框宽度=2,浮雕=凹槽,背景='steelblue'
)
self.frame.pack(fill=X,expand=False)
self.frame.pack_传播(False)
self.label=标签(self.frame\
text='想要看到“退出”按钮完美地工作吗?',bg='steelblue')
self.label.pack(padx=25,pady=15,side=TOP)
self.root.event_add(“”,“”,“”,“”)
self.root.bind(“”,lambda e:self.bye_bye())
self.byeWindow=无
def CreateQuit按钮(自身):
pbx=250
pby=70
pbw=75
pbh=25
pbp=10
self.quitBtn=按钮(
自我框架,
text='Quit',
activebackground='steelblue3',
font='arial 10粗体',
bg='steelblue',
command=self.bye\u bye
)
自我退出(
x=pbx,
y=pby,
宽度=pbw,
高度=pbh
)
pbx+=pbw+pbp
def dontQuit(自身、tlref):
tlref.destroy()
self.byeWindow=无
def bye_bye(自我):
如果self.byeWindow不是None:
返回
self.byeWindow=Toplevel()
self.byeWindow.title(“真的辞职了?”)
self.byeWindow.config(bg='steelblue',高度=40,宽度=80)
sureMsgLabel=标签(self.byeWindow,
text=“你确定要退出吗?”,
font='arial 11粗体',
bg='steelblue'
)
sureMsgLabel.pack(侧面=顶部,padx=10,pady=15)
yesButton=按钮(self.byeWindow,
text=“是。”,
font='arial 10粗体',
bg='steelblue',
activebackground='steelblue3',
command=lambda:quit()
)
yesButton.pack(侧=左,锚=N,padx=40,pady=20)
绑定(“”,lambda:yesButton.invoke())
noButton=按钮(self.byeWindow,
text=“编号”,
activebackground='steelblue3',
font='arial 10粗体',
bg='steelblue',
command=lambda:self.dontquiit(self.byeWindow)
)
诺布顿,集中力量
noButton.pack(侧=右,锚=N,padx=40,pady=20)
绑定(“”,lambda:noButton.invoke())
bT=按钮测试类()
bT.createQuitButton()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
bT.root.mainloop()
实际上,它本身并不是一个程序,而是从一个有“按钮不工作”问题的程序中提取的几行代码。干杯 结果表明,归根结底,我的问题是一个实例变量的拼写错误:“byWindow”而不是“byeWindow”,这在我的self.bye_bye()函数中阻塞了if语句。纠正拼写纠正了这个问题。非常感谢和亲切问候布赖恩·奥克利,感谢他提出的富有洞察力的建议和睿智的建议。这是完美运行的程序
from Tkinter import *
class ButtonTestClass(Frame):
def __init__(self):
self.root=Tk()
self.root.title("Button Tester!")
self.root.option_add('*font', ('arial', 14, 'bold'))
self.frame=Frame(self.root, height=120, width=600, \
borderwidth=2, relief=GROOVE, background='steelblue'
)
self.frame.pack(fill=X, expand=False)
self.frame.pack_propagate(False)
self.label = Label(self.frame, \
text='Wanna see a "Quit" button working flawlessly?', bg='steelblue')
self.label.pack(padx=25, pady=15, side=TOP)
self.root.event_add('<<ev_quit>>', '<Control-c>', '<Control-q>', '<q>')
self.root.bind('<<ev_quit>>', lambda e: self.bye_bye())
self.byeWindow = None
def createQuitButton(self):
pbx=250
pby=70
pbw=75
pbh=25
pbp=10
self.quitBtn=Button(
self.frame,
text='Quit',
activebackground='steelblue3',
font='arial 10 bold',
bg='steelblue',
command=self.bye_bye
)
self.quitBtn.place(
x=pbx,
y=pby,
width=pbw,
height=pbh
)
pbx += pbw + pbp
def dontQuit(self, tlref):
tlref.destroy()
self.byeWindow = None
def bye_bye(self):
if self.byeWindow is not None:
return
self.byeWindow=Toplevel()
self.byeWindow.title("Really quit?")
self.byeWindow.config(bg='steelblue', height=40, width=80)
sureMsgLabel=Label(self.byeWindow,
text="Are you sure you want to quit?",
font='arial 11 bold',
bg='steelblue'
)
sureMsgLabel.pack(side=TOP, padx=10, pady=15)
yesButton=Button(self.byeWindow,
text=" Yes. ",
font='arial 10 bold',
bg='steelblue',
activebackground='steelblue3',
command=lambda: quit()
)
yesButton.pack(side=LEFT, anchor=N, padx=40, pady=20)
yesButton.bind('<Key-Return>', lambda: yesButton.invoke())
noButton = Button(self.byeWindow,
text=" No. ",
activebackground='steelblue3',
font='arial 10 bold',
bg='steelblue',
command=lambda: self.dontQuit(self.byeWindow)
)
noButton.focus_force()
noButton.pack(side=RIGHT, anchor=N, padx=40, pady=20)
noButton.bind('<Key-Return>', lambda: noButton.invoke())
bT = ButtonTestClass()
bT.createQuitButton()
if __name__ == '__main__':
bT.root.mainloop()
从Tkinter导入*
类按钮测试类(框架):
定义初始化(自):
self.root=Tk()
self.root.title(“按钮测试器!”)
self.root.option_add('*font',('arial',14',bold'))
self.frame=frame(self.root,高度=120,宽度=600\
边框宽度=2,浮雕=凹槽,背景='steelblue'
)
self.frame.pack(fill=X,expand=False)
self.frame.pack_传播(False)
self.label=标签(self.frame\
text='想要看到“退出”按钮完美地工作吗?',bg='steelblue')
self.label.pack(padx=25,pady=15,side=TOP)
self.root.event_add(“”,“”,“”,“”)
self.root.bind(“”,lambda e:self.bye_bye())
self.byeWindow=无
def CreateQuit按钮(自身):
pbx=250
pby=70
pbw=75
pbh=25
pbp=10