Reputation: 101
Hello, I'm struggling using multiple tkinter windows in python. Basicly, I have two classes which are also related to two different windows. The main class shows the main window (parentWindow) and the other class shows a secondWindow (childWindow). The following code starts the MainWindow:
#START THE APPLICATION
root = Tkinter.Tk()
root.title ("GEMEINDESTECKBRIEF-Menü")
# My main Application
runGUI = MainWorkspaceConfig (root)
root.mainloop ()
So far there aren't any problems!
Now I'm trying to open a second Window calling a function in the Main Class (kind of onClickFunction to open the Window)
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()
Still everything works just fine!! The window opens as expected and also the GUI-widgets are displayed and useable. After finishing the given tasks the Window has to be closed and HERE ALL TROUBLE STARTS!!! To quit the Window I'm using a button with a command function which looks like this:
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()
The self.tkinterFrame.quit() closes not just the secondWindow (childWindow) it also closes the MainWindow (parentWindow) too. The self.tkinterFrame.destroy() function clears all widget from the window but the window still is active and visible!!
So, any Ideas how to solve the problem? Would be thankful for any solutions!!!!!
Upvotes: 1
Views: 3758
Reputation: 1
Try this self.Frame1.destroy()
Or whatever your frame name maybe
Sometimes you could have this
self.Frame1 = tk.Frame(top)
Upvotes: 0
Reputation: 101
YEESSS, Finally I found the solution to my issue!!!!
First Step: In the Main Class which starts the ChildWindow I changed the code in the function def opendirFactsheetHochwasserGebaeude (self) :
from Tkinter.Tk()
to Tkinter.Toplevel(parent)
=> the parent references the root Window. After changing the Tkinter typ the facthwgeb.mainloop()
was also cleared because it is provided by the MainWindow (Parent)
Second Step: In the Second Class which implements the ChildWindow the function def closeFactsheetHochwGeb (self):
did privously owned the commands self.tkinterFrame.destroy()
which cleared the widget of the frame but not the window itself and the self.tkinterFrame.quit()
closes the MainWindow and the ChildWindow => so both commands are useless!!
Final Step: The final resolution is to change the self.tkinterFrame.destroy ()
to self.tkinterFrame.master.destroy()
!!
Sometimes complex things can be very simple!! :-)
Upvotes: 0