How to open multiple windows of the same file in Tkinter? - python

I have a simple application in python. When I click a button it should open up the same file multiple times. However, after two times the program won't open any more windows.
Here is my code so far for file1:
from tkinter import *
root = Tk()
root.geometry("600x600")
def newWin():
import file1
button = Button(root, text="Open Window of same file", command=newWin)
button.pack()
root.mainloop()
After clicking the button one time, it opens up the same file in a new window, but when I click the button in that window, it doesn't work. How do I fix this?

import file1 will only import file1 and execute the code inside file1 once. When import file1 is called again, nothing will happen because file1 has already been imported.
To get around this, you can put the code inside a function and call that function after importing file1:
# file1.py
import tkinter as tk
def main():
root = tk.Tk()
root.geometry('600x600')
def new_win():
import file1
file1.main()
button = tk.Button(root, text='Open Window of same file', command=new_win)
button.pack()
root.mainloop()
if __name__ == '__main__':
main()
However, importing file1 inside file1 is not a good practice and should be avoided.
The above code is just a demo of fixing the import issue. Actually you don't need to call import file1 inside file1, just call main():
# file1.py
import tkinter as tk
def main():
root = tk.Tk()
root.geometry('600x600')
button = tk.Button(root, text='Open Window of same file', command=main)
button.pack()
root.mainloop()
if __name__ == '__main__':
main()

Your button has no command parameter, otherwise, it will do nothing.
button = Button(root, text="Open Window of same file", command = newWin)
Also, change add the .py extension while importing your file:
import file1.py

Related

how can i open multiple windows in tkinter in another file?

i have a problem in my code in python (tkinter)
I have two files (F1.py) and (F2.py), each of which is a window, and I have another file called (main.py) that opens a window with two buttons, I want to click one of the (F1) files every time Or (F2) open.
What should I do ?
this is my code:
from tkinter import *
main = Tk()
def f1():
import f1.py
# I do not know what to do to run the file f1.py here !!
def f2():
import f2.py
# I do not know what to do to run the file f2.py here !!
btn_f2 = Button(main,text="open f2",command=f2).pack()
btn_f1 = Button(main,text="open f1",command=f1).pack()
mainloop()
Here is the work around according to your need, Since you want to execute your other tkinter window from your main python script. Here is the way to do that.
in your main.py
from tkinter import *
import os
import sys
import f1,f2
main= Tk()
def open(filename):
os.chdir("D:\\PYTHON_FILES\\") #change this path to your path where your f1.py and f2.py is located
# print("current dir "+os.getcwd())
os.system('python '+filename) #runnning the python command on cmd to execute both windows
btn_f2 = Button(main,text="open f2",command=lambda: open("f1.py")).pack()
btn_f1 = Button(main,text="open f1",command=lambda: open("f2.py")).pack()
main.mainloop()
Now define f1.py for example
from tkinter import *
window= Tk()
window.title('f1 Hello Python')
window.geometry("300x200+10+10")
mainloop()
Similarly for example we have f2.py
from tkinter import *
window= Tk()
window.title('f2 Hello Python')
window.geometry("300x200+10+10")
mainloop()
Now If you want to go for the legit way using TopLevel in Tinkter as #Matiis Described then here is clean and perfect way to achieve the goal.
Simply make a main.py as below
from tkinter import *
root = Tk()
root.geometry("200x200")
#here define your f1 window
def f1():
top = Toplevel(root)
top.geometry("400x400")
top.title("I am f1 window smaller than f2 but bigger than root")
top.mainloop()
#Similarly here define your f2 window
def f2():
top = Toplevel(root)
top.geometry("500x500")
top.title("I am f2 window bigger than f1")
top.mainloop()
btn1 = Button(root, text = "open f1", command = f1)
btn2 = Button(root, text = "open f2", command = f2)
btn1.place(x=75,y=50)
btn2.place(x=75,y=20)
root.mainloop()
I might be a little late with this, but I've recently started learning python and I had the same issue with tkinter. I had a "main.py" file and I wanted it to open "f1.py" by clicking a button.
I realized that when you configure a button in tkinter you have to assign a function so I wrote "main.py" as a regular pyhon/tkinter file importing f1.py:
from tkinter import *
from tkinter import ttk
import f1
main = Tk()
main.title("Main window")
but = Button(main, text="Open f1.py", command= f1.f1_func)
but.pack()
main.mainloop()
And instead of defining the function on the main file (def but_openwindow(): etc...) I wrote that function on the "f1.py" but JUST as a function, not as a mainloop. So the f1.py would be something like this:
from tkinter import *
from tkinter import ttk
def f1_func():
other = Toplevel()
other.title("Other window")
text = Label(other, text="This is another window")
text.pack()
And you could repeat it for any number of other .py files. So far I found out 2 important things:
1- I thought it wouldn't be necessary, but apparently you have to start f1.py importing tkinter libraries, otherwise it wouldn"t work. At least for me it didn't.
2- The files that you want to import can't have a "mainloop" or it just opens the other window as soon as you execute the main file.
I have yet to try to make an .exe of the main file, but I'm hoping to get there.
Sorry again for being late, hope it helps, if not for you, at leas for anyone else that might encounter this same problem.

Trying to run .ahk files in tkinter

this is my first time here so if I break some unwritten rules please don't shoot me
I've been trying build an app for editing, reading and opening .ahk files. The last one gives me problems.
this code kind of does what I want except it fires the command immediately after opening the app.
from tkinter import *
from tkinter import filedialog
import os
import threading
root = Tk()
def openfile():
os.system( r'\Users\merijn\PycharmProjects\infoprojectP3\venv\Scripts\ahkScript1.ahk')
def func():
print("hello")
btn1 = Button(text='programma', command=threading.Thread(target=openfile).start())
btn2 = Button(text='ander ding', command=func)
btn1.pack()
btn2.pack()
root.mainloop()
Any help would be much appreciated!
Change the following line (it just assigns the result of threading.Thread(...) to command option):
Button(text="run program1", command=threading.Thread(target=lambda: os.system(r'\Users\merijn\PycharmProjects\infoprojectP3\venv\Scripts\ahkScript1.ahk').start()))
to
Button(text="run program1", command=lambda: threading.Thread(target=lambda: os.system(r'\Users\merijn\PycharmProjects\infoprojectP3\venv\Scripts\ahkScript1.ahk')).start())
or
Button(text="run program1", command=lambda: threading.Thread(target=os.system, args=[r'\Users\merijn\PycharmProjects\infoprojectP3\venv\Scripts\ahkScript1.ahk']).start())
Same for buttons of run program2 and run program3.
Update: based on your updated code, you need to change the following line:
btn1 = Button(text='programma', command=threading.Thread(target=openfile).start())
to
btn1 = Button(text='programma', command=lambda: threading.Thread(target=openfile).start())

How do you execute another file when a button is pressed(tkinter) in Python?

I'm trying to make a launcher for another program but I just started with Python so I made a button, but I struggle to figure out how to execute another .py file. Any help?
When the button is pressed it activates the open_file() function and os opens the .py script.
from tkinter import *
import os
def open_file():
os.system('python file path here')
root=Tk()
btn = Button(root, text='Open .PY File', command=open_file)
btn.pack()
root.mainloop()
Here is a solution using from subprocess import call. All you have to do is replace 'YOUR_FILE_NAME' with... your file name :D
from tkinter import *
from subprocess import call
root=Tk()
root.geometry('200x100')
frame = Frame(root)
frame.pack(pady=20,padx=20)
def Open():
call(["python", "YOUR-FILE-NAME.py"])
btn=Button(frame,text='Open File',command=Open)
btn.pack()
root.mainloop()
What it will look like:
I hope this works for you :D

My Tkinter Files wont open after 3 tries. Please Assist me

Basically I created two Tkinter files with buttons. When I click on one button it should take me to the other file and when I click a button the other file it should take me back. However, it's not working after 3 tries. Please help me find the solution for this. For more detailed question watch this video: https://drive.google.com/file/d/1Ma_-szEf4JBbxwFgiSS5GrkIFrWI3Mog/view
Here is my code also:
File 1:
from tkinter import *
root.title("Hello This is file 1")
root.geometry("600x600")
def goToFileTwo():
root.destroy()
import file2
button = Button(text="Click here to go to file 2", command=goToFileTwo)
button.pack()
root.mainloop()
File 2:
from tkinter import *
root.title("Hello This is file 2")
root.geometry("600x600")
def goToFileOne():
root.destroy()
import file1
button = Button(text="Click here to go to file 1", command=goToFileOne)
button.pack()
root.mainloop()
Please help me find a solution.
In the file1 and file2 you just add the below code, this will delete your imported module from system cache
del sys.modules["your_module_name"]
please don't forget to import sys module also
so your full code of file1 will be
from tkinter import *
import sys
root=Tk()
root.title("Hello This is file 1")
root.geometry("600x600")
try:
del sys.modules["file2"]
except:
None
def goToFileTwo():
root.destroy()
import file2
button = Button(text="Click here to go to file 2", command=goToFileTwo)
button.pack()
root.mainloop()

Tkinter window not closing after closed file dialog

I would like to close the File Open dialog after selecting a file. Currently with my code, I can select a file but the File Open dialog remains open until I click the 'X'. How can I close this window after I have selected a file.
Here is my code:
import sys
from tkinter import *
from tkinter.filedialog import askopenfilename
fname = "unassigned"
def openFile():
global fname
fname = askopenfilename()
if __name__ == '__main__':
b = Button(text='File Open', command = openFile).pack(fill=X)
mainloop()
print (fname)
The file dialog is closing just fine. I think what you are trying to say is that the Tkinter window you created to hold the button is not closing after you select a file from the dialog. To have it do this, you will need to restructure your program a bit.
First, you need to explicitly create a Tk window to hold the button:
root = Tk()
You should then list this window as the button's parent:
Button(root, text='File Open', command = openFile).pack(fill=X)
# ^^^^
Finally, you should call the destroy method of the root window at the end of openFile:
root.destroy()
This will cause the window to close and the Tkinter mainloop to exit.
In all, your script will look like this:
import sys
from tkinter import *
from tkinter.filedialog import askopenfilename
fname = "unassigned"
def openFile():
global fname
fname = askopenfilename()
root.destroy()
if __name__ == '__main__':
root = Tk()
Button(root, text='File Open', command = openFile).pack(fill=X)
mainloop()
print (fname)

Categories