繁体   English   中英

python tkinter自动打开子窗口

[英]python tkinter automatically opens child window

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

该程序运行正常,只是它会自动打开子窗口

不要调用函数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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM