Home > Software design >  Linking 2 tkinter GUI with a main file
Linking 2 tkinter GUI with a main file

Time:02-11

I'm trying to link 2 tkinter file in a main file, I'm relatively quite new for python When i try to run my main file my tkinter window does not pop out. if i run individually it will run page2 instead of page 1 what is wrong inside here ?

Here is my main file

import page1w,page2w
from tkinter import *

def main():
    page1_window=page1w
    page2_window=page2w

Here is my page 1 tkinter file

from tkinter import *
import page2w
root=Tk()

root.geometry("500x500")
root.title("Some title")

#Label
l1=Label(root, text="Label1:")
l2=Label(root, text="Label2:")
l3=Label(root,text="Label 3")

#Input from above
e1=Entry(root)
e2=Entry(root)
e3=Entry(root)

#submit button
s1=Button(root,text='Submit',command=page2w)

#Arrangement of label & input
l1.grid(row=0,column=0,sticky=W,pady=2)
l2.grid(row=1,column=0,sticky = W,pady=2)
l3.grid(row=2,column=0,sticky=W , pady=2)
e1.grid(row=0,column=1,pady=2)
e2.grid(row=1,column=1,pady=2)
e3.grid(row=2,column=1,pady=2)
s1.grid(row=3,column=3,pady=2)


mainloop()

here is my page 2 file

from tkinter import *

root=Tk()

root.geometry("500x500")
root.title("Some title")

#Label
l1=Label(root, text="Label1 ")
l2=Label(root, text="Label2 ")
l3=Label(root,text="Label3")

#Pass & Fail Button
l1b=Button(root,text="PASS")
l2b=Button(root,text="PASS")
l3b=Button(root,text="PASS")
l1bf=Button(root,text="FAIL")
l2bf=Button(root,text="FAIL")
l3bf=Button(root,text="FAIL")

#Next button
s1=Button(root,text='Next',)

#Arrangement of label & input
l1.grid(row=0,column=0,sticky=W,pady=2)
l2.grid(row=1,column=0,sticky = W,pady=2)
l3.grid(row=2,column=0,sticky=W , pady=2)
l1b.grid(row=0,column=4,sticky=E)
l2b.grid(row=1,column=4,sticky=E)
l3b.grid(row=2,column=4,sticky=E)
l1bf.grid(row=0,column=5,sticky=E)
l2bf.grid(row=1,column=5,sticky=E)
l3bf.grid(row=2,column=5,sticky=E)
s1.grid(row=3,column=3,pady=2)

mainloop()

i'm using python3.xx what is a suitable way to link all tkinter gui & run in sequence order say : page1w then page2 etc,etc

CodePudding user response:

For your case, you don't need the main file actually. Just run page1w.py as the main file and modify page1w.py as below:

from tkinter import *

def page2():
    # destroy current window
    root.destroy()
    # open page2 window
    import page2w

root=Tk()
...
s1=Button(root,text='Submit',command=page2)
...
mainloop()

CodePudding user response:

Your functions are running when you import them. You can test this by running

import page1w,page2w
from tkinter import *

# def main():
#     page1_window=page1w
#     page2_window=page2w

as your main script. This is because you should be defining your code as functions or a class in the scripts you are importing. This gives:

page1w:

from tkinter import *
import page2w

def page1w(root):
    root.geometry("500x500")
    root.title("Some title")

    #Label
    l1=Label(root, text="Label1:")
    l2=Label(root, text="Label2:")
    l3=Label(root,text="Label 3")

    #Input from above
    e1=Entry(root)
    e2=Entry(root)
    e3=Entry(root)

    #submit button
    s1=Button(root,text='Submit',command=page2w)

    #Arrangement of label & input
    l1.grid(row=0,column=0,sticky=W,pady=2)
    l2.grid(row=1,column=0,sticky = W,pady=2)
    l3.grid(row=2,column=0,sticky=W , pady=2)
    e1.grid(row=0,column=1,pady=2)
    e2.grid(row=1,column=1,pady=2)
    e3.grid(row=2,column=1,pady=2)
    s1.grid(row=3,column=3,pady=2)

if __name__=="__main__":
    root=Tk()
    page1w(root)
    mainloop()

page2w:

from tkinter import *

def page2w(root):
    root.geometry("500x500")
    root.title("Some title")

    #Label
    l1=Label(root, text="Label1 ")
    l2=Label(root, text="Label2 ")
    l3=Label(root,text="Label3")

    #Pass & Fail Button
    l1b=Button(root,text="PASS")
    l2b=Button(root,text="PASS")
    l3b=Button(root,text="PASS")
    l1bf=Button(root,text="FAIL")
    l2bf=Button(root,text="FAIL")
    l3bf=Button(root,text="FAIL")

    #Next button
    s1=Button(root,text='Next',)

    #Arrangement of label & input
    l1.grid(row=0,column=0,sticky=W,pady=2)
    l2.grid(row=1,column=0,sticky = W,pady=2)
    l3.grid(row=2,column=0,sticky=W , pady=2)
    l1b.grid(row=0,column=4,sticky=E)
    l2b.grid(row=1,column=4,sticky=E)
    l3b.grid(row=2,column=4,sticky=E)
    l1bf.grid(row=0,column=5,sticky=E)
    l2bf.grid(row=1,column=5,sticky=E)
    l3bf.grid(row=2,column=5,sticky=E)
    s1.grid(row=3,column=3,pady=2)

if __name__=="__main__":
    root=Tk()
    page2w(root)
    mainloop()

However, your main file still won't run properly. This is because you need to create an instance of Tk() and pass it to both functions

from page1w import *
from page2w import *
from tkinter import *

def main(root):
    page1_window=page1w(root)
    page2_window=page2w(root)

root = Tk()
main(root)
root.mainloop() 

This will run both of your windows, but they will be on the same window, which (I think) is not your intention. We can get around this by creating a Toplevel() instance. You should not create a second Tk(); while it might work, it actually creates two tcl interpreters in the background and means that the two windows can't interact with each other.

from page1w import *
from page2w import *
from tkinter import *

def main(first_window, second_window):
    page1_window=page1w(first_window)
    page2_window=page2w(second_window)

root = Tk()
second_window = Toplevel(root)
main(root, second_window)
root.mainloop() 

Your code will now be running each of your imported files in a seperate window. Hopefully this is enough to help you for now, but I recommend following along some tutorials on tkinter

  • Related