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!!!!!
Don't make a second Tk() instance; you can/should have only one root.
Use a Toplevel widget for facthwgeb instead. Also, get rid of the facthwgeb.mainloop() call, as again, there should only be one call to this.
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!! :-)
Try this self.Frame1.destroy()
Or whatever your frame name maybe
Sometimes you could have this
self.Frame1 = tk.Frame(top)
Related
I created a button in tkinter with its command being to create anew window. But I don't want it to create a new window every time I click the create a new window button. I want to tell the program to open the new window only if it is not already open. If somewhere in the background the window is already open and the user presses the create a new window button I want it to move that window on the first layer of the user's screen (bringing it upfront from all the other open windows).
Here is my code --
def main():
main_bg = "Gray"
main_fg = "White"
import tkinter as tk
employees = []
window = tk.Tk()
window.configure(bg=main_bg)
window.title("Business app")
window.geometry("1100x650")
def open_new_window():
random_window = tk.Toplevel(window)
random_window.configure(bg=main_bg)
random_window.geometry("600x600")
random_button = tk.Button(window, text="Do Something", font="Times 32", bg=main_bg, fg=main_fg, command=open_new_window)
random_button.pack()
window.mainloop()
if __name__ == "__main__":
main()
I have searched websites like Geeksforgeeks and this website but I could not find the solution I was looking for. Please help.
NOTE -- PLEASE DO NOT CLOSE THIS QUESTION AND POINT ME TOWARDS ANOTHER FORUM BECAUSE I HAVE CHECKED OTHERS IN THIS WEBSITE AS I MENTIONED ABOVE
Added grab_set() will prevent open new window again.
def open_new_window():
random_window = tk.Toplevel(window)
#random_window.configure(bg=main_bg)
random_window.geometry("600x600")
random_window.grab_set()
Result:
When you click Do Something will prevent open new window. Unless close the topelevel window and then open new window again
You can save a reference to the widget, and then check the reference to see if it's set. If it is set, you can then call winfo_exists to check whether the reference points to an existing window.
random_window = None
...
def open_new_window():
global random_window
if random_window and random_window.winfo_exists():
# the window exists, so do nothing
return
random_window = tk.Toplevel(window)
random_window.configure(bg=main_bg)
random_window.geometry("600x600")
So I've given myself a little project and I'm trying to make a little tool to connect to the OKEX exchange. Now I'm currently working on the GUI and I've decided to use Tkinter. After lots of research and what-not, I've come up with the following, but now I've become a bit stuck.
I've got 2 classes, one for the main window and one for the login window. However, some of the functions of the main window rely on what happens after I've submitted the login details. Now I know that Toplevel is for creating additional windows in Tkinter, and you normally close these windows with .destroy() , and if I want to pick up on this event in the main window class then I need to use the Toplevel.protocol("WM_DELETE_WINDOW", function_name) call ...but this isn't working for me.
It works as expected if I close using the cross in the top right, but not if I close with my function that calls .destroy() , can anyone explain to me why this isn't working as intended? Perhaps I have missed something ?
I want to change the text in the first frame to "Logged In" after the user (me) enters in their details, but I need to login first and pass through a user object to contain those details.
Anyways, here's the code, please help me out!
The code in question is the myquit function in the LoginBox class , and the goToLogin function in the MainBox class :)
from tkinter import *
from tkinter.ttk import *
class LoginBox:
def __init__(self, master): # Master is the frame that is passed in.
# Create Frame
self.master = master
self.master.title('~ Please Login ~')
def login_function(self):
user = "xxx"
secret_key = "yyy"
print("User - API Key: " + user)
print("User - Secret Key: " + secret_key)
# Perhaps check the login ...
# if it's a success then quit the function
self.myquit()
def myquit(self):
self.master.destroy()
class MainBox:
def __init__(self, master):
# Set the root window
self.master = master
self.master.geometry("500x500")
self.master.title("OkBot v0.1")
self.master.resizable(False, False)
# Initialize the frames
self.uiFrame1 = Frame(self.master) # The Top Layer -- Login Text + Login Button
# uiFrame1 Initialize --Login Text + Login Button
self.ui1_button = Button(self.uiFrame1, text="Login", command=self.goToLogin).grid(row=0, column=3, sticky=E, padx=1)
# Create Topview for popup , pass in User Object to LoginBox
def goToLogin(self):
loginMaster = Toplevel(self.master)
loginMaster.protocol("WM_DELETE_WINDOW", self.checkLogin) # This is if they close via X
loginGUI = LoginBox(loginMaster)
def checkLogin(self):
print("This function was called -- The Protocol for destroyed windows works")
# Initialize the objects and start the program
mainWindow = Tk()
myProgram = MainBox(mainWindow)
mainWindow.mainloop()
It works as expected if I close using the cross in the top right, but not if I close with my function that calls .destroy() , can anyone explain to me why this isn't working as intended? Perhaps I have missed something ?
loginMaster.protocol("WM_DELETE_WINDOW", self.checkLogin) only tells tkinter what to do when the window manager destroys the window. When you call some function which calls destroy(), that doesn't involve the window manager and therefore your callback is not called.
If you want something to happen when the window is destroyed, you should bind to the <Destroy> event.
I have a tkinter gui and I would like it to retain the original window position and size upon relaunching.
Here is an answer that illustrates how to set a specific position and dimensions, but not a word about remembering the settings: How to specify where a Tkinter window opens?
Highly appreciate any help.
The only way to remember settings from session to session is to write them into a file. So, get the root window geometry (it's a string) and write it into a file. If you want the function to be executed automatically as a hook, bind it to the "<Configure>" event:
def save_size(event):
with open("myapp.conf", "w") as conf:
conf.write(root.geometry()) # Assuming root is the root window
root.bind("<Configure>",save_size)
You can later read the geometry from the file and restore it.
#Here I save the x and y position of the window to a file "myapp.conf"
#Here I see if the file exists.
if os.path.isfile("myapp.conf"):
#Here I read the X and Y positon of the window from when I last closed it.
with open("myapp.conf", "r") as conf:
root.geometry(conf.read())
else:
#Default window position.
root.geometry('610x270+0+0')
def on_close():
#custom close options, here's one example:
#close = messagebox.askokcancel("Close", "Would you like to close the program?")
#if close:
#Here I write the X Y position of the window to a file "myapp.conf"
with open("myapp.conf", "w") as conf:
conf.write(root.geometry())
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_close)
It took me pretty much time to get my head around actual implementation of this. So I wanted to share my final code. Based on DyZ suggestion.
I didn't use <Configure> event as suggested because only saving before quit is enough for me.
class Window(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# ...
# bla bla bla
# My GUI codes came here
# ...
# Try to load last saved window data
self.statusbar['text']="reading ini file..."
ini_file_path = "mapedit.ini"
try:
# if the file is there
# get geometry from file
ini_file = open(ini_file_path,'r')
self.geometry(ini_file.read())
self.statusbar['text']= "ini file read"
ini_file.close()
except:
# if the file is not there, create the file and use default
# then use default geometry.
self.statusbar['text']="ini file not found. New file created."
ini_file = open(ini_file_path, 'w')
ini_file.close()
self.geometry("640x400+100+200")
def client_exit(self):
self.save_geo()
self.destroy()
def save_geo(self):
# save current geometry to the ini file
try:
with open("mapedit.ini", 'w') as ini_file:
ini_file.write(self.geometry())
self.statusbar['text']="geo sv"
ini_file.close()
except:
statusbar['text']="ini file not found"
'''
This is where I created GUI instance
'''
if __name__ == "__main__":
win = Window()
# attach deletion handler to 'client_exit' method
win.protocol("WM_DELETE_WINDOW", win.client_exit)
win.mainloop()
Below is part of my code. The problem I'm having is I want to insert a value to a Tkinter entry in my main window, but when I call the variable 'selected_db' I get the value of A. I want to get the value after the 'OK button' is clicked so I get the value of B
I'm not sure how should I do it or if this is even close to the right way to do it so I would really appreciate some help here.
Thanks in advance
Main_Window():
def browse_db(self):
my_dbs = Databases()
self.db_entry.insert(0,my_dbs.selected_db)
def __init__(self):
self.main_window = Tkinter.Toplevel()
self.db_entry = ttk.Entry(self.main_window, width=10)
self.db_entry.grid(row=1, column= 1)
Databases():
def __init__(self):
self.selected_db = A
self.db_window = Tkinter.Toplevel()
okButtom = ttk.Button(self.db_window, command=self.grab_db).grid(row=1, column=1)
def grab_db(self):
self.selected_db = B
It's hard to tell without a complete, working example, but I think what you are asking is how to make your main window wait for your popup to exit. In other words what we usually call a "modal window". To do that you need to pass the master window to the Toplevel call and add these commands at the end of the popup's __init__ method:
self.transient(master) # set to be on top of the main window
self.grab_set() # hijack all commands from the master (clicks on the main window are ignored)
master.wait_window(self) # pause anything on the main window until this one closes
Ok, so this is from a larger project that I am working on, so I apologize if it looks messy.
The issue is that when I click the 'Exit Program' Button on the GUI, the window remains active.
I know that the button is working as when I hit the 'x' on the top right corner the window; the program closes, so the run variable has been set back to 0, which stops the code from looping.
My question is how do I get the window to be closed automatically when the exit button is clicked, because the root.destroy() method isn't doing it.
#imports
from tkinter import *
import random, pickle, shelve
#global vars
run = 0
class Window(Frame):
#the class that manages the UI window
def __init__(self, master, screen_type = 0):
"""Initilize the frame"""
super(Window, self).__init__(master)
self.grid()
if screen_type == 1:
self.log_in_screen()
def log_in_screen(self):
#Program Exit Button
self.exit = Button(self, text = " Exit Program ", command = self.end)
self.exit.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def end(self):
global run, root
run = 0
root.destroy()
#Main Loop
def main():
global run, root
run = 1
while run != 0:
root = Tk()
root.title("Budget Manager - 0.6.1")
root.geometry("400x120")
screen = Window(root, screen_type = run)
root.mainloop()
store = shelve.open("store.dat", "c")
main()
store.close()
My question is how do I get the window to be closed automatically when
the exit button is clicked, because the root.destroy() method isn't
doing it.
The answer is: call destroy() on the root window. You say it isn't working, but the code you posted seems to work, and what destroy() is documented to do is exactly what you describe you want to have happen: it will destroy the window. Your code creates new toplevel windows in a loop, so maybe it only appears to not work since the old window id destroyed and the new window is created in the blink of an eye.
It seems like what you're really asking is "how can I make clicking on the "x" do the same as clicking on the "Exit program" button?". If that is the case, the answer is very straight-forward, even with your unconventional code that creates root windows in a loop.
To get the "x" button on the window frame to call a function instead of destroying the window, use the wm_protocol method with the "WM_DELETE_WINDOW" constant and the function you want it to call.
For example:
while run != 0:
root = Tk()
...
screen = Window(root, screen_type = run)
root.wm_protocol("WM_DELETE_WINDOW", screen.end)
...
root.mainloop()
you could do something like the below. I've used it in my own projects etcand it works.
Mywin =tkinter.Tk()
def exit():
Mywin.quit()
# etc.