Tha*_*anc 3 python tkinter python-2.7
from Tkinter import *
class Window(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.geometry("600x400+30+30")
wButton = Button(self, text='text', command = self.OnButtonClick())
wButton.pack()
def OnButtonClick(self):
top = Toplevel()
top.title("title")
top.geometry("300x150+30+30")
topButton = Button(top, text="CLOSE", command = self.destroy)
topButton.pack()
if __name__ == "__main__":
window = Window(None)
window.title("title")
window.mainloop()
# top.lift(aboveThis=self)
#self.configure(state=DISABLED) - unknown option "-state"
#ss = self.state()
#self["state"] = "disabled" - unknown option "-state"
#ws = window.state() # >>> ws outputs: 'normal'
# varname.unbind("<Button-1>", OnButtonClick)
#self.unbind("<Button-1>", OnButtonClick)
#window.unbind("<Button-1>")
###if window.OnButtonClick == True:
### window.unbind("<Button-1>", OnButtonClick)
Run Code Online (Sandbox Code Playgroud)
上面的Python ver2.7.3代码,当在IDLE ver2.7.3中运行时,使用Tkver8.5:在显示一个window = Window(Tk)的实例之前,首先显示较小的top = Toplevel()窗口一秒钟.这是在点击任何按钮或任何按钮之前.
上面代码中的所有注释都只是对我自己尝试过的事情以及下一步尝试的想法(idk - 也许是无用的东西).
如何将上面的代码更改为:将window = Window(Tk)的实例设为父窗口,将top = Toplevel()窗口设为子窗口.然后,当我运行程序时,只显示父窗口; 然后当我点击'wButton'时,子窗口应显示在父窗口的顶部,父窗口被禁用 - 它的按钮无法操作,用户无法通过点击窗口将窗口抬到最前面?
command只需要函数名称 - 不带()参数.
使用
wButton = Button(self, text='text', command = self.OnButtonClick)
Run Code Online (Sandbox Code Playgroud)
如果您使用command = self.OnButtonClick()run self.OnButtonClick(),则分配结果command.如果要command动态创建函数,它可能非常有用.
要在顶级父窗口中使用子窗口,您可以使用 child.transient(parent)
在你的代码中它应该是 top.transient(self)
def OnButtonClick(self):
top = Toplevel()
top.title("title")
top.geometry("300x150+30+30")
top.transient(self)
topButton = Button(top, text="CLOSE", command = self.destroy)
topButton.pack()
Run Code Online (Sandbox Code Playgroud)
您可以使用.config(state='disabled')和.config(state='normal')禁用/启用按钮.
您可以禁用主窗口按钮OnButtonClick()但需要新功能以在子窗口关闭之前/之后启用该按钮.
from Tkinter import *
class Window(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.geometry("600x400+30+30")
self.wButton = Button(self, text='text', command = self.OnButtonClick)
self.wButton.pack()
def OnButtonClick(self):
self.top = Toplevel()
self.top.title("title")
self.top.geometry("300x150+30+30")
self.top.transient(self)
self.wButton.config(state='disabled')
self.topButton = Button(self.top, text="CLOSE", command = self.OnChildClose)
self.topButton.pack()
def OnChildClose(self):
self.wButton.config(state='normal')
self.top.destroy()
if __name__ == "__main__":
window = Window(None)
window.title("title")
window.mainloop()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14180 次 |
| 最近记录: |