When the below code is run there's an additional, empty window appears. Why does that happen, and how can it be fixed?
import tkinter as tk
class Deneme(tk.Tk):
def __init__(self):
super().__init__()
self.smartGui()
def smartGui(self):
tk.Label(self, text="Name").grid(row=0,column=0)
tk.Entry(self).grid(row=0,column=1, columnspan=2)
tk.Label(self, text="Surname").grid(row=1,column=0)
tk.Entry(self).grid(row=1,column=1, columnspan=2)
tk.Label(self, text="City").grid(row=0,column=4)
tk.Entry(self).grid(row=0,column=5, columnspan=2)
tk.Label(self, text="Explain").grid(row=3, column=0)
def main():
root = tk.Tk()
app = Deneme()
root.mainloop()
if __name__ == "__main__":
main()
Simplest way would be to replace:
def main():
root = tk.Tk()
app = Deneme()
root.mainloop()
with:
def main():
app = Deneme()
app.mainloop()
There are two windows displayed, as there are two instances of Tk, root and app. app is an instance of Tk as well, as Deneme inherits from Tk.
Note: It's not suggested to have multiple instances of Tk. If you'd want multiple windows, later on, you should use Toplevel instead of Tk.
Additionally, you could simply inherit Deneme from Frame instead of Tk but you'd want to configure it a bit more for it to allow you to assign its parent as root. Then create its instance like app = Deneme(root).
Related
I am writing an application in tkinter consisting of several modules in which there are classes. Each module to a separate page of the app. As I move the buttons between the pages "next", "previous" it opens a new window for me every time. How do I make it so that each time calling pages opens in the same window?
I give draft code.
thank you for your help :D
task1.py
import tkinter as tk
from Test.modul.task1 import FirstPage1
class FirstPage0:
def __init__(self, root):
self.root = root
def get_settings(self):
# Window settings
self.root.geometry("100x200")
def get_second_page(self):
FirstPage1(tk.Toplevel()).get_run_first_page()
def get_button(self):
# Add buttons
tk.Button(self.root, text="Start page", command=self.get_second_page).pack()
tk.Button(self.root, text="Exit", command=self.root.destroy).pack()
def get_run_first_page(self):
# Launching the application
self.get_settings()
self.get_button()
self.root.mainloop()
if __name__ == '__main__':
first = FirstPage0(tk.Tk())
first.get_run_first_page()
task2.py
import tkinter as tk
class FirstPage1:
def __init__(self, root):
self.root = root
def get_settings(self):
# Window settings
self.root.geometry("100x200")
def get_second_page1(self):
from Test.task import FirstPage0
FirstPage0(tk.Toplevel()).get_run_first_page()
def get_button(self):
# Add buttons
tk.Button(self.root, text="Back", command=self.get_second_page1).pack()
tk.Button(self.root, text="Exit", command=self.root.destroy).pack()
def get_run_first_page(self):
# Launching the application
self.get_settings()
self.get_button()
self.root.mainloop()
if __name__ == '__main__':
first = FirstPage1(tk.Tk())
first.get_run_first_page()
Solution
#AdrianSz, you wanted to make the buttons not stack under each other. There are three ways to do so. One, is to keep only one button and change its command and text parameters each time when the frames change. Another would be to unpack the button not needed and pack the button needed. The third would be to pack the buttons in the root window instead of frame and change the text and command parameters. I would recommend the second method as it is easier and less prone to errors.
Code
task1.py
import tkinter as tk
from Test.modul.task1 import FirstPage1
class FirstPage0:
def __init__(self, root):
self.root = root
def get_settings(self):
# Window settings
self.root.geometry("100x200")
def get_second_page(self):
self.root.pg_0_btn_start.pack_forget()
self.root_pg_0_btn_exit.pack_forget()
FirstPage1(tk.Toplevel()).get_run_first_page()
def get_button(self):
# Add buttons
self.root.pg_0_btn_start = tk.Button(self.root, text="Start page",
command=self.get_second_page).pack()
self.root_pg_0_btn_exit = tk.Button(self.root, text="Exit",
command=self.root.destroy).pack()
def get_run_first_page(self):
# Launching the application
self.get_settings()
self.get_button()
self.root.mainloop()
if __name__ == '__main__':
first = FirstPage0(tk.Tk())
first.get_run_first_page()
task2.py
import tkinter as tk
class FirstPage1:
def __init__(self, root):
self.root = root
def get_settings(self):
# Window settings
self.root.geometry("100x200")
def get_second_page1(self):
from Test.task import FirstPage0
self.root.pg_1_btn_back.pack_forget()
self.root_pg_1_btn_exit.pack_forget()
FirstPage0(tk.Toplevel()).get_run_first_page()
def get_button(self):
# Add buttons
self.root.pg_1_btn_back = tk.Button(self.root, text="Back", command=self.get_second_page1).pack()
self.root.pg_1_btn_exit = tk.Button(self.root, text="Exit", command=self.root.destroy).pack()
def get_run_first_page(self):
# Launching the application
self.get_settings()
self.get_button()
self.root.mainloop()
if __name__ == '__main__':
first = FirstPage1(tk.Tk())
first.get_run_first_page()
Note: I couldn't test this code from my side so if there are any errors, please comment on this answer
Suggestions
Since you are using modules for these, just make them inherit a class specified in a different file and operate them both from that file. There you can use self to access the methods of a subclass because the subclasses instantiate the base class and thus the self is a object of the subclass and is passed to the base class. The type of code you used is quite confusing too. I have added code to give you your wanted output using the principles I mentioned here. Hope this helped!
I have this simple code with self.btn1
from tkinter import Tk, ttk, messagebox
import tkinter as tk
class Main(tk.Frame):
def __init__(self, root):
super().__init__(root)
self.btn1 = ttk.Button(self, text="test")
self.btn1.pack()
if __name__ == "__main__":
root = tk.Tk()
app = Main(root)
app.pack()
root.mainloop()
and this code without self button
from tkinter import Tk, ttk, messagebox
import tkinter as tk
class Main(tk.Frame):
def __init__(self, root):
super().__init__(root)
btn1 = ttk.Button(self, text="test")
btn1.pack()
if __name__ == "__main__":
root = tk.Tk()
app = Main(root)
app.pack()
root.mainloop()
Both of them work similarly, but what's the difference, which one should I use?
The only real difference lies in how easy it is to retrieve the reference to the Button instance should you need one. With the former, it's just app.btn1. With the latter, it's app.winfo_children()[0].
>>> app.winfo_children()[0] is app.btn1
True
The difference is that btn1 is a local variable that is only available in the function in which it is used. self.btn1 is an instance variable that is available in every function within the class.
I created a couple of GUIs using tkinter. But now I am interested in combining them into one caller GUI. So the caller GUI would have buttons that, when clicked, would open the other GUIs. However, I cannot get it to work. I've done the imports correctly (I think), edited the main functions in the subGUIs, and added the command=GUI.main in my buttons. I get it to load but I get errors about missing files...but when I run a GUI by itself it works fine.
In my research, I read that there can only be one mainloop in a Tkinter program. Basically, I cannot use a Tkinter GUI to call another Tkinter GUI. Do you know what I can do different, for instance, can I create the caller GUI using wxPython and have it call all other GUIs that use Tkinter?
Thank you!
You can't "call" another GUI. If this other GUI creates its own root window and calls mainloop(), your only reasonable option is to spawn a new process. That's a simple solution that requires little work. The two GUIs will be completely independent of each other.
If you have control over the code in both GUIs and you want them to work together, you can make the base class of your GUI a frame rather than a root window, and then you can create as many windows as you want with as many GUIs as you want.
For example, let's start with a simple GUI. Copy the following and put it in a file named GUI1.py:
import tkinter as tk
class GUI(tk.Frame):
def __init__(self, window):
tk.Frame.__init__(self)
label = tk.Label(self, text="Hello from %s" % __file__)
label.pack(padx=20, pady=20)
if __name__ == "__main__":
root = tk.Tk()
gui = GUI(root)
gui.pack(fill="both", expand=True)
tk.mainloop()
You can run that GUI normally with something like python GUI1.py.
Now, make an exact copy of that file and name it GUI2.py. You can also run it in the same manner: python GUI2.py
If you want to make a single program that has both, you can create a third file that looks like this:
import tkinter as tk
import GUI1
import GUI2
# the first gui owns the root window
win1 = tk.Tk()
gui1 = GUI1.GUI(win1)
gui1.pack(fill="both", expand=True)
# the second GUI is in a Toplevel
win2 = tk.Toplevel(win1)
gui2 = GUI2.GUI(win2)
gui2.pack(fill="both", expand=True)
tk.mainloop()
Depending on your OS and window manager, one window might be right on top of the other, so you might need to move it to see both.
Thank you for the ideas. At first, your code wouldn't print the text on the toplevel window. So I edited it a little and it worked! Thank you. GUI1 and GUI2 look like:
import tkinter as tk
def GUI1(Frame):
label = tk.Label(Frame, text="Hello from %s" % __file__)
label.pack(padx=20, pady=20)
return
if __name__ == "__main__":
root = tk.Tk()
GUI1(root)
root.mainloop()
And then the caller looks like this:
from tkinter import *
import GUI1
import GUI2
def call_GUI1():
win1 = Toplevel(root)
GUI1.GUI1(win1)
return
def call_GUI2():
win2 = Toplevel(root)
GUI2.GUI2(win2)
return
# the first gui owns the root window
if __name__ == "__main__":
root = Tk()
root.title('Caller GUI')
root.minsize(720, 600)
button_1 = Button(root, text='Call GUI1', width='20', height='20', command=call_GUI1)
button_1.pack()
button_2 = Button(root, text='Call GUI2', width='20', height='20', command=call_GUI2)
button_2.pack()
root.mainloop()
Preface:
I have a Python ControlGPIO code with a working GUI (let us call it MainGUI).
I wish to have a dialog pop up window, prior to running MainGUI, in a way that the user can enable/ disable features in MainGUI. BUT MainGUI should start running only after dialog pop up window is closed.
My question is: How can I make a popup window that will postpone MainGUI untill it is closed?
Code below- boot_windows is my dialog pop up window (where all the enable/disable checkboxes will be ), BUT obviously does not postpone App as I need
root = Tk()
#output_gpioPins = [4,22,6,26]
#input_gpioPins = [3,21,5,27]
#ip = '192.168.2.112'
boot_windows = Toplevel(root)
text1 = ttk.Label(boot_windows, text="Hello World !!!")
text1.grid()
App = ContorlGPIOWindow(root, ip = '192.168.2.113', with_sf_bt=1, with_hw_bt=1, switch_names=['Light Kitchen','Light Room1', 'Window1', 'Window2'])
root.mainloop()
You can't do precisely what you want. Widgets exist in a tree-like structure. All windows except the root require a root window. The root window must be created first (which is why it's called the root window).
If you don't want the user to see it, you can hide it until it is ready to be displayed.
import tkinter as tk
root = tk.Tk()
root.withdraw()
boot_window = tk.Toplevel(...)
...
You can then call root.deiconify() when you are ready for it to be visible.
Another common solution is to use the root window for your dialog or splash screen or whatever, and then simply replace its contents with the real contents when you're ready.
As for how to wait for the popup... the root window has a method named wait_window which will enter the event loop and not return until the given window has been destroyed.
Here's an example of it's use:
import Tkinter as tk
class MainGUI(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Hello, world!")
label.pack(fill="both", expand=True, padx=20, pady=20)
class Popup(tk.Toplevel):
def __init__(self, root):
tk.Toplevel.__init__(self, root)
label = tk.Label(self, text="Click to continue...")
label.pack(fill="both", expand=True, padx=20, pady=20)
button = tk.Button(self, text="OK", command=self.destroy)
button.pack(side="bottom")
if __name__ == "__main__":
root = tk.Tk()
root.withdraw()
popup = Popup(root)
root.wait_window(popup)
main = MainGUI(root)
main.pack(fill="both", expand=True)
root.deiconify()
root.mainloop()
I've got a Tkinter application that I'm writing in Python 3.
I've got one module which contains a class in which I create all my widgets. I'm trying to update a progress bar from a separate module that does the heavy lifting.
#GUI.py
import tkinter as tk
from tkinter.filedialog import askopenfilename, askdirectory
import tkinter.ttk as ttk
import Work_Module
progress_bar = None
class Application(tk.Frame):
def __init__(self, master=None):
master.title('CCD Creator Version 1.0')
tk.Frame.__init__(self, master, width=500)
self.pack()
self.create_widgets()
def create_widgets(self):
global progress_bar
progress_bar = tk.ttk.Progressbar(orient=tk.HORIZONTAL, length=200, mode='determinate')
progress_bar.pack(side="bottom")
self.run = tk.Button(self, text=" Run ", fg="green", command=self.do_work)
self.run.pack(side="top")
#Continue creating various widgets that don't need to be updated by other modules
def do_work(self):
Work_Module.main()
def main():
root = tk.Tk()
app = Application(master=root)
app.mainloop()
if __name__ == '__main__':
main()
Here is the module with a class that does the "heavy lifting" that I want to track the progress on.
#Work_Module.py
class Work:
def __init__(self):
#Do some intensive work
main():
for idx, job in enumerate(work_list):
Work()
import GUI
percentage_done = idx / len(job_list) * 100
GUI.progress_bar.step(percentage_done)
The problem I'm running into is that progress_bar has a value of None in the Work_Module. It's assigned an object in the GUI module, but that object isn't retained when trying to reference it from the Work_Module. I know that importing GUI in the main method is kind of strange, and I suspect that this is some sort of namespace issue or circular import problem, but I can't seem to think of a way to work around this.
One way is to inject the progress_bar into Work_Module just before you call the mainloop():
def main():
root = tk.Tk()
app = Application(master=root)
Work_Module.progress_bar = progress_bar
app.mainloop()
If you like, you can also dispense with the global aspect of the progress_bar:
def create_widgets(self):
# global progress_bar (no longer needed)
# add 'self'
self.progress_bar = tk.ttk.Progressbar(orient=tk.HORIZONTAL, length=200, mode='determinate')
self.progress_bar.pack(side="bottom")
...
def main():
root = tk.Tk()
app = Application(master=root)
Work_Module.progress_bar = self.progress_bar # add 'self'
app.mainloop()