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
Related
This is my current code.
from tkinter import *
from tkinter import ttk, filedialog
from tkinter.filedialog import askopenfile
import os
def main():
path = open_file()
print(path)
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of tkinter frame
win.geometry("700x350")
def open_file():
file = filedialog.askopenfile(mode='r', filetypes=[('PDF', '*.pdf')])
if file:
filepath = os.path.abspath(file.name)
quit()
print(filepath)
def quit():
win.destroy()
# Add a Label widget
label = Label(win, text="Click the Button to browse the Files", font=('Georgia 13'))
label.pack(pady=10)
# Create a Button
ttk.Button(win, text="Browse", command=open_file).pack(pady=20)
win.mainloop()
if __name__ == '__main__':
main()
I want to create a simple GUI to select a file and then use its path in other functions later on. In the current code the filepath prints out fine after the window closes in the open_file function, but it only works if the print statement is in the function. If I remove it from there and want to print it out(just to test) or use it further to pass it to other functions from main it doesn't seem to work. There is no error but it doesn't print anything either. Any idea?
The problem appears to be is wanting to return a value from a function called with a button. This is apparently not possible. The easiest solution I've found (without changing too much of your code structure) is to use a global variable for filepath. Now you can use it pretty much anywhere you want.
Also, I've removed path = open_file() from your main() function as it is redundant. The function is now called only when the button is pressed and not at every start of the program.
from tkinter import *
from tkinter import ttk, filedialog
from tkinter.filedialog import askopenfile
import os
def main():
print(filepath)
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of tkinter frame
win.geometry("700x350")
def open_file():
file = filedialog.askopenfile(mode='r', filetypes=[('PDF', '*.pdf')])
if file:
global filepath
filepath = os.path.abspath(file.name)
quit()
def quit():
win.destroy()
# Add a Label widget
label = Label(win, text="Click the Button to browse the Files", font=('Georgia 13')) label.pack(pady=10)
# Create a Button
ttk.Button(win, text="Browse", command=open_file).pack(pady=20)
win.mainloop()
if __name__ == '__main__':
main()
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()
I have been searching for a solution for this issue, but I can't seem to find a viable answer.
I have the following code to open another tkinter script on button click:
# Program to Open on Button Click
def tkinter1():
ret = os.system('python C:\filepath\new_script.py"')
if ret:
# record updated, reload data
treeview_preview()
b1 = Button(master, text="New Window", command=tkinter1)
My problem I am facing is that I want the current window to close on the button click and only keep the new window open.
I know it is possible. I have this instance with many different windows and I seem to be stuck.
The unfortunate thing is that I have an entire different script for different parts of the beta software and the only way I could successfully run all of them is to access them as stated above.
I tried using the if ret: exit() command at the end with the same result. I have windows opening over and over again.
It seems simple, but I haven't been programming tkinter script for too long.(I still have a lot to learn)
All help is appreciated.
You can use master.withdraw(). I
made your code runnable with a few lines. Ok lets say your fixed main code is this:
import tkinter as tk
from tkinter import *
from seccond import *
from multiprocessing import Process
import os
def main():
master = tk.Tk()
# Program to Open on Button Click
def tkinter1():
master.withdraw()
master.destroy()
ret = Process(target=treeview_preview())
ret.start()
#if ret:
# # record updated, reload data
# treeview_preview()
#commented out bc i don't know what it is
b1 = Button(master, text="New Window", command=tkinter1)
b1.pack()
master.mainloop()
if __name__ == '__main__':
main()
the secondary code name seccond.py in the same folder as the main code is called as an external function but also as a separate process.
import tkinter as tk
def treeview_preview():
root = tk.Tk()
S = tk.Scrollbar(root)
T = tk.Text(root, height=4, width=50)
S.pack(side=tk.RIGHT, fill=tk.Y)
T.pack(side=tk.LEFT, fill=tk.Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
quote = """this is the seccond window."""
T.insert(tk.END, quote)
tk.mainloop()
The code will remove the window but not the terminal box as it will use it for the second script.
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()
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.)