单击按钮打开另一个窗口的 Python GUI 代码中断

标签 python python-2.7 tkinter

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)

上面的 Python ver2.7.3 代码,当在 IDLE ver2.7.3 中运行时,使用 Tkver8.5:首先显示较小的 top=Toplevel() 窗口 其次,在显示上面的 window=Window(Tk) 的一个实例之前 它。这是在点击任何按钮或任何东西之前。

上面代码下面的所有评论只是我自己对我尝试过的事情和下一步尝试的想法的注释(idk - 可能是无用的东西)。

我如何将上面的代码更改为:使 window=Window(Tk) 的实例成为父窗口,使 top=Toplevel() 窗口成为子窗口。然后,当我运行程序时,应该只显示父窗口;然后当我点击'wButton'时,子窗口应该出现在父窗口的顶部,父窗口被禁用 - 它的按钮无法操作并且用户无法通过点击它使窗口提升到最前面?

最佳答案

command 只需要函数名 - 没有 () 和参数。

使用

wButton = Button(self, text='text', command = self.OnButtonClick)

如果您使用 command = self.OnButtonClick(),您将运行 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()

您可以使用 .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()

关于单击按钮打开另一个窗口的 Python GUI 代码中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20180536/

相关文章:

Python请求模块Put问题

python - 在 Python 中动态地将父类(super class)添加到已知类

python - 如何设置SocketServer的最大连接数

python - 将变量传递给环境

python - 哪些事件可以绑定(bind)到 Tkinter Frame?

python - 使用 Tkinter 添加标准对象作为 Frame 类属性

python-3.x - 结合 tkinter 和看门狗

python - Django REST 框架 : SlugRelatedField for indirectly-related attribute?

python - re.sub 字符串 : (? : . ..) 的一部分

python - 我只需要 "identity url"吗? - OpenID