Tkinter command function call - python

I am new with python ,
And since i started python the command call in Tkinter never works for me
I have tried all the ways and i took of the brakets off but it doesn't work still
i have seen the related topics here but they didn't work for me
here is a simple code that i have tried noting that when i clic the button nothing happens so where is the problem coming from?
from tkinter import *
if __name__ == "__main__":
root = Tk()
root.geometry("400x400")
compile_button=Button(root,text="Compiler",command=root.quit)
compile_button.pack()

Add root.mainloop() at the end of your __main__. So the GUI starts working. For further info check out this question.
You can also use root.destroy method.
The whole code:
from tkinter import *
if __name__ == "__main__":
root = Tk()
root.geometry("400x400")
compile_button=Button(root,text="Compiler", command=root.quit)
compile_button.pack()
root.mainloop()

Related

Can't get tkinter tabs to show

I decided I want to learn how to make GUIs with something more than entry fields and buttons, so I'm starting off with tabs. After a little bit of research I made myself a program, but don't know why it doesn't work.
# --IMPORTS--
from tkinter import *
import tkinter.ttk as ttk
import time
# --CLASSES--
class Gui:
def __init__(self):
self.root = Tk()
self.root.title("tab test")
def setup(self):
# tabs
tabc = ttk.Notebook(self.root)
tab1 = ttk.Frame(tabc)
tabc.add(tab1, text="test 1")
tabc.grid()
def run(self):
self.root.mainloop()
# --MAIN--
if __name__ == "__main__":
gui = Gui()
gui.run()
When I run the program I just get a blank screen (see screenshot) and there is no way to see if there is a tab, let alone which one is selected.
Like I said, I don't see the reason why it isn't working. There are no error messages to point me in the right direction and I'm not 100% sure on how tabs work to begin with, so I thought I'd ask here. I've tried changing .grid() to .pack() but I think it's more of an error on my end than a bug with tkinter. Thanks in advance!
you have to run your setup method.
# --MAIN--
if __name__ == "__main__":
gui = Gui()
gui.setup()
gui.run()

Using Python, how do you call a tkinter GUI from another GUI?

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

Tkinter is opening multiple GUI windows upon file selection with multiprocessing, when only one window should exist

I have primary.py:
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import ttk
import multiprocessing as mp
import other_script
class GUI:
def __init__(self, master):
self.master = master
def file_select():
path = askopenfilename()
if __name__ == '__main__':
queue = mp.Queue()
queue.put(path)
import_ds_proc = mp.Process(target=other_script.dummy, args=(queue,))
import_ds_proc.daemon = True
import_ds_proc.start()
# GUI
root = Tk()
my_gui = GUI(root)
# Display
frame = Frame(width=206, height=236)
frame.pack()
ttk.Button(frame, text="Select", command=file_select).pack(anchor='nw')
# Listener
root.mainloop()
And other_script.py:
def dummy(parameter):
pass
When running this, upon selection of a file, a second GUI window appears. Why? This is undesired behavior, I instead want dummy to run.
Thanks.
Just like with multiprocessing, you need to place the code to do with tkinter and making your window within the entry point to your program (such that it is not ran more than once through another process).
This means moving the if __name__ == "__main__" clause to the 'bottom' of your program and placing the code to do with tkinter in there instead. The entry point to your multiprocessing will still be protected as it is called after an event, which is defined within the start point.
Edit:
The entry point is where your program is entered from, normally when you say if __name__ == "__main__".
By moving the tkinter code into the entry point, I mean something like this:
if __name__ == "__main__":
root = Tk()
my_gui = GUI(root)
frame = Frame(width=206, height=236)
frame.pack()
ttk.Button(frame, text="Select", command=file_select).pack(anchor='nw')
root.mainloop()
(At the 'bottom' of your program, i.e. after all functions are defined.)

Tkinter: messagebox doesn`t work right. (get two windows)

I am using python 3.
If I opan an error messagebox, i get two frames, one is emty and one is the error-window. That is my code:
from tkinter import messagebox
messagebox.showwarning('warning', 'warning')
Everything works correctly in your example. The empty window is the main window of Tk. It is always open when you start any Tk program. You can minimize it if you want, but closing it terminates the main loop.
Try this:
root = tkinter.Tk()
root.withdraw()
messagebox.showwarning('warning', 'warning')
Thank you DYZ,
in my code is no main window, (eg.: main = Tk() ... main.mainloop), because of that the warning massage create one. I could solve the problem by create one and minimize it. at the end of massagebox I destroyed it to continue in code.
from tkinter import *
from tkinter import messagebox
main = Tk()
main.geometry("500x400+300+300")
def message():
main.geometry("0x0")
messagebox.showwarning("Say Hello", "Hello World")
main.destroy()
B1 = Button(main, text = "Start Dialog",fg="dark green", command = message)
B1.pack()
main.mainloop()
print("finish dialog")

Calling another GUI from a file in python

Suppose I have one GUI with a simple code as follows
It has a button on it and when this is clicked I want to have another GUI to pop-up and then call the function from it. The problem is that when I run the first file, the GUI of the other one automatically pop-up. What should I do.
The code of the first file is as follows
from tkinter import *
import another
root = Tk()
button1 = Button(root, text = "Call" , command = another.abc)
button1.pack()
root.mainloop()
The code of second file another.py is as follows
from tkinter import *
root_Test2 = Tk()
root_Test2.geometry('450x450')
def abc():
print("that's working")
root_Test2.mainloop()
Please suggest the solution that help me to open the second window when the button on the first one is clicked
According to #PM 2Ring, You can change your second file's code to this:
from tkinter import *
if __name__ == '__main__':
root_Test2 = Tk()
root_Test2.geometry('450x450')
def abc():
print("that's working")
if __name__ == '__main__':
root_Test2.mainloop()
You can find further information about if __name__ == '__main__' here

Categories

Resources