This code is not mine, it is from Byan Oakley
import tkinter as tk
from tkinter import font as tkfont
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is the start page", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame("PageOne"))
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame("PageTwo"))
button1.pack()
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 1", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 2", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
I dont see any root = Tk()
so I can't use the .geometry
and I think that .geometry
is not included when using import tkinter
so I can't set a size for the window. Since there are 3 different pages, would I have to set a size for each page or can a global size be set? I'm new to python and started to get into tkinter 2 days ago, thanks for any help!
The way the imports are done is largely irrelevant. No matter how you import tkinter, to change the geometry of any tkinter window you call the geometry
method of the window.
In this case the root window is app
because SampleApp
inherits from Tk
.
From outside the class:
app = SampleApp()
app.geometry("800x600")
Or, inside the SampleApp
class:
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
...
self.geometry("800x600")
...
app = SampleApp
is defined at the bottom. If you call app.geometry()
immediately after, it works.
If you follow the code, you arrive in class SampleApp()
which is inherited from tk.Tk
. Therefore, here you can also put geometry()
, but now as self.geometry()
because this class IS a tk.Tk
object which does have the geometry
property.
I agree that -at first sight- it may be a bit confusing because in many cases you'll see something like root = tk.Tk()
at the beginning, so you could refer to root
(or app, or master). But it is completely valid though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.