【问题标题】:Python GUI code to click a button to open another window breaksPython GUI 代码单击一个按钮打开另一个窗口中断
【发布时间】:2013-12-09 10:32:21
【问题描述】:
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) 的一个实例之前 它。这是在单击任何按钮或任何内容之前。

上面代码下面的所有 cmets 只是对我自己的笔记,我已经尝试过的事情和接下来要尝试的想法(idk - 也许是无用的东西)。

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

【问题讨论】:

  • 请验证显示代码的正确性(缩进)。另外,请尽量让您的问题简短,但要切中要害。
  • 代码现在显示在上面,就像我在 *.py 文件中输入的一样。感谢您修复我的格式错误。我猜以前是一些不正确的缩进弄乱了它,因为当我把帖子放在 *.html 文件中时,它显示正确。我将删减非代码、问题部分,然后编辑帖子。

标签: python python-2.7 tkinter


【解决方案1】:
谢谢你,弗拉斯!

“命令”只需要一个函数名 - 没有“()”和参数。 感谢您在那里发现我的错误。我认为这就是“神秘地”改变了我以前得到的结果的原因。我对“命令”选项进行了更改,当我将其更改回来时,我错误地输入了“()”。所以,我想结果(顶部已经绘制)就是我得到的。关于为“命令”动态创建函数的见解确实非常有用。我要记住这一点。

你的四个建议效果很好。哈,我记得想弄清楚如何改变窗口的状态很长时间,但我找不到任何示例代码,所以我没有得到正确的语法,即使在网上查看和查看 *.py带有源代码的模块。我非常感谢您向我展示了解决方案。

【讨论】:

    【解决方案2】:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-07
      • 2016-02-28
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多