繁体   English   中英

Python Tkinter子窗口问题

[英]Python Tkinter Child Window Issue

您好 ,我正在努力在python中使用多个tkinter窗口。 基本上,我有两个与两个不同窗口有关的类。 主类显示主窗口(parentWindow),另一类显示secondWindow(childWindow)。 以下代码启动MainWindow:

#START THE APPLICATION
root = Tkinter.Tk()
root.title ("GEMEINDESTECKBRIEF-Menü")
# My main Application
runGUI = MainWorkspaceConfig (root)
root.mainloop ()

到目前为止没有任何问题!

现在,我试图打开第二个在Main Class中调用函数的窗口(类似于onClickFunction来打开窗口)

def opendirFactsheetHochwasserGebaeude (self) :
    #validates the workspace resp. database directory and
    #print self.checkFactsheet2.get()
    #print self.inputSpace1.get()

    try:
        if self.checkFactsheet2.get()==1 :

            if self.inputSpace1.get() or self.inputSpace2.get() != "":
                 #write workspace environment to __initFile__
                if self.inputSpace1.get() != "":
                    self.writeWorkspEnv(self.inputSpace1.get())
                    #Copy file in seperate thread
                    start_new_thread(self.copyDefaultFactoWorkspace,())
                if self.inputSpace2.get() != "":
                    self.writeWorkspEnv(self.inputSpace2.get())
                # !!!!!!! START SECOND WINDOW !!!!!
                facthwgeb = Tkinter.Tk()
                facthwgeb.title ("Factsheet Hochwasser-Gebäude")
                runGUI = Factsheet_hochwassergebaeude (facthwgeb)
                facthwgeb.mainloop ()
                #facthwgeb.protocol('WM_DELETE_WINDOW', runGUI.closeFactsheetHochwGeb)
            else:
            #self.inputSpace1.get() and self.inputSpace2.get () =="":
                tkMessageBox.showwarning ("Keine Arbeitsumgebung festgelegt", "Bitte entweder einen neuen Workspace anlegen oder eine bestehende Datenbank auswählen!")
                self.selectBox1.deselect()

仍然一切正常! 该窗口将按预期方式打开,并且GUI小部件也将显示并可用。 完成给定任务后,必须关闭窗口,然后在这里进行所有故障排除!!! 要退出窗口,我使用带有命令功能的按钮,如下所示:

   def closeFactsheetHochwGeb (self):

    try:
        if self.inputSpace1.get() and self.inputSpace2.get() != "":

            with open('%s/__initFile__.txt'%os.path.dirname(os.path.realpath(__file__)), 'r') as file:
                    # read a list of lines into data
                    data = file.readlines()
                    data[13] = self.inputSpace1.get()+"\n"
                    data[14] = self.inputSpace2.get()+"\n"
                    # and write everything back
            with open('%s/__initFile__.txt'%os.path.dirname(os.path.realpath(__file__)), 'w') as file:
                    file.writelines( data )
                    file.close()
            # self.tkinterFrame.destroy()
            self.tkinterFrame.quit()

self.tkinterFrame.quit()不仅关闭secondWindow(childWindow),还关闭MainWindow(parentWindow)。 self.tkinterFrame.destroy()函数从窗口清除所有小部件,但窗口仍处于活动状态且可见!

那么,有什么想法可以解决问题吗? 感谢任何解决方案!!!!!

不要再创建一个Tk()实例; 您可以/应该只有一个根。

使用的Toplevel的窗口小部件facthwgeb代替。 同样,摆脱facthwgeb.mainloop()调用,同样,对此应该只有一个调用。

YEESSS ,终于找到了解决我问题的方法!!!!

第一步:在启动ChildWindow的主类中,我更改了函数def opendirFactsheetHochwasserGebaeude (self) :的代码def opendirFactsheetHochwasserGebaeude (self) :Tkinter.Tk()Tkinter.Toplevel(parent) =>父级引用了根Window。 更改Tkinter的facthwgeb.mainloop() ,也会清除facthwgeb.mainloop() ,因为它是由MainWindow(父类)提供的

第二步:在实现ChildWindow的Second Class中,函数def closeFactsheetHochwGeb (self):私下拥有命令self.tkinterFrame.destroy() ,该命令清除了框架的小部件,但清除了窗口本身以及self.tkinterFrame.quit()关闭MainWindow和ChildWindow =>,所以两个命令都没有用!

最后一步:最后的解决方案是将self.tkinterFrame.destroy ()更改为self.tkinterFrame.master.destroy()

有时复杂的事情可能非常简单! :-)

尝试使用此self.Frame1.destroy()或任何您的框架名称,有时您都可以拥有此self.Frame1 = tk.Frame(top)

暂无
暂无

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

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