0

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()
6
  • It doesn't make sense to say that your files "are windows" or that you want to "run" them. You need to think more clearly about the problem before you can solve it. If your other files include their own mainloop() call and other stuff that happens immediately, then you need to redesign. Also, you don't use the .py filename extension in an import statement; you're naming the module, not the file. I think you may need to take a few steps back and make sure you understand fundamentals properly first. Designing a GUI is not an easy task. Commented Mar 31, 2021 at 22:13
  • also this does not seem like a great way to run a file since it probably would be great to close them and open again and for example what happens if You press the button twice and other stuf Commented Mar 31, 2021 at 22:16
  • from what I have seen You have to create a class (or a function but class is probalby more prefferable) and then import that and launch windows from there, for that You can use TopLevel. more info about tkinter here Commented Mar 31, 2021 at 22:18
  • also could You add the code from other files? Commented Mar 31, 2021 at 22:18
  • The key here is to have all of the code in f1.py that creates the window be inside a function called f1, and the same with f2. Then your file can start with from f1 import f1 and from f2 import f2. Commented Mar 31, 2021 at 22:34

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

8 Comments

thanks this code work's but when i want a make exe file with main.py this didn't work.
See if you're going with the first approach from the answer then that is not feasible or say legit way to handle GUIs, Since now you want to make exe of your program so the second approach from the program is viable or correct way . So the second approach will work perfect even after making exe. Let me know if you're still facing issue
i try second approach but i gave this error from code: image "pyimage5" doesn't exist'
What is the module you're using for creating your exe?
im using pyinstaller module
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.