【问题标题】:python tkinter automatically opens child windowpython tkinter 自动打开子窗口
【发布时间】:2014-03-04 10:04:45
【问题描述】:

我是 python 新手,我必须编写某种 gui。但是 gui 由子窗口组成,目前只有一个子窗口。问题是在启动时子窗口也会启动。它应该等到单击按钮然后启动子窗口。我不知道它为什么会这样......

#!/usr/bin/env python

from Tkinter import *
import tkMessageBox as box
import rospy


class gui(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, background="white")
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Baxter analyse tool")
            menubar = Menu(self.parent)
        self.parent.config(menu=menubar)
            fileMenu = Menu(menubar)
        submenu = Menu(fileMenu)
        submenu.add_command(label="camera tool", command=self.camera_window())
        submenu.add_command(label="range tool")
        submenu.add_command(label="control tool")
        submenu.add_command(label="sonar tool")
        submenu.add_command(label="quick check tool")
        fileMenu.add_cascade(label="tools", menu=submenu, underline=0)
        fileMenu.add_separator()
        fileMenu.add_command(label="Exit", command=self.onExit)
        menubar.add_cascade(label="File", menu=fileMenu)
        menubar.add_command(label="about", command=self.about)

    def camera_window(self):
        cameraGui = CameraGui()

    def about(self):
        box.showinfo("Baxter","Analyse tool.")

    def onExit(self):
        self.quit() 

class CameraGui(object):
    def __init__(self):
        self.initUI()

    def initUI(self):
        win = Toplevel()
        Label(win, text="testestest").pack()
        Button(win, text="hello", command=win.destroy).pack()           

def main():
    rospy.init_node('baxter_lput_analyse_tool')
    root = Tk()
    root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth()/2, root.winfo_screenheight()-50))
    root.focus_set()
    root.bind("<Escape>", lambda e: e.widget.quit())
    app = gui(root)
    root.mainloop()

if __name__=='__main__':
    main()

程序运行良好,只是它会自动打开子窗口

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    不要调用函数self.camera_window()。 删除()。你的 self.camera_window 方法会在主循环开始后立即被调用。

    这样做:

    submenu.add_command(label="camera tool", command=self.camera_window)

    或者如果你想发送一些参数然后:

    submenu.add_command(label="camera tool", command=lambda:self.camera_window(args))

    【讨论】:

    • 谢谢!那成功了。但是为什么我必须删除括号?
    • 因为当你包含括号时,你实际上是在调用方法而不是等待menubutton被按下
    • 哦,是的,我有点傻。无论如何感谢您的帮助!
    猜你喜欢
    • 2013-09-18
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2018-11-17
    • 2019-08-07
    • 1970-01-01
    相关资源
    最近更新 更多