Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Go to next page TKinter
#1
hello,

i have a little question to my code. i have 2 files in the same folder, script 1 and script 2
code in script 1:
from tkinter import *

ws = Tk()
ws.geometry('400x300')
ws.title('PythonGuides')
ws['bg']='#5d8a82'

f = ("Times bold", 14)

def nextPage():
    import page2

Label(
    ws,
    text="This is First page",
    padx=20,
    pady=20,
    bg='#5d8a82',
    font=f
).pack(expand=True, fill=BOTH)

Button(
    ws, 
    text="next page", 
    font=f,
    command=nextPage
    ).pack(fill=X, expand=TRUE, side=LEFT)

ws.mainloop()
and script 2
from tkinter import *

ws = Tk()
ws.geometry('400x300')
ws.title('PythonGuides')
ws['bg']='#ffbf00'

f = ("Times bold", 14)
 

Label(
    ws,
    text="This is Second page",
    padx=20,
    pady=20,
    bg='#ffbf00',
    font=f
).pack(expand=True, fill=BOTH)



ws.mainloop()
my question is, why can i only click next page once?
the second time i click nothing happens.
it would be nice if the second time you click the window opens again
is this somehow possible? I sit at the problem already some days and come on no solution
I thank you first of all

achim1024768
Reply
#2
(Jun-27-2022, 05:20 PM)achim1024768 Wrote: my question is, why can i only click next page once?
the second time i click nothing happens.

I've not run your code, but I assume you mean that after the first instance of script 2 has run run (by importing it) you close down that script and can't run it again in the same way (by importing it)?

If that's the case, then (so far as I know) therein is the answer: once 'imported', it won't be imported again, within the same session, that is to say, you'd need to quit script 1 (thus dropping the import) before script 2 can be imported again.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
You can only call Tk() once in a tkinter application. Tk() does not just create a window, it also initializes the tkinter framework. If you want a GUI with multiple windows use Tk() to create the root window and Toplevel() to make additional top level windows.

You should also investigate using only one window, but changing the view. Tkinter has a Frame widget which is like a window that appears inside another window. A application can create multiple frames, and switch which of these frames appear in the window.

But even if you were using top level windows for additional windows, your idea wouldn't work. The loader only loads modules once.
file1.py
import file2
print("file1")
file2.py
import file1
print("file2")
Output:
file1 file2 file1
The example above executes file1.py twice; once because it is imported by file2.py, and once because I executed it from the command line "python file1.py". file2.py executes once when it is imported by file1.py. After the first import the module is imported and does not need to be imported again. Even if I modify file1.py and file2.py to do multiple inputs the output remains the same.
file1.py
import file2
print("file1")
import file2
file2.py
import file1
print("file2")
import(file1)
Output:
file2 file1 file2
If a module contains code that you want to execute multiple times, wrap the code in a function and call the function.
file1.py
import file2
file2.doit()
file2.doit()
file2.doit()
file2.py
def doit():
    print("file2")
Output:
file2 file2 file2
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020