Tkinter button "clickbox" detached from widget in fullscreen - python-3.x

I am on MacOS 10.15.7 and have just found that when I run my application, the buttons work perfectly fine no matter how large I make the window. However, when I make the window go into fullscreen mode, the buttons register clicks inside a box about 20 pixels below where the widget shows. Has anyone else come across this issue? I'm using the grid geometry manager, with sticky="we" so I don't think that the way I've added the buttons is the problem. I have also called grid_rowconfigure(i, weight=1) on every row of my application to perform the appropriate vertical alignment when stretching.
Here is a small snippet that shows how I've structured the application.
import tkinter as tk, tkinter.ttk as ttk
class Application(tk.Tk):
def __init__(self):
super().__init__()
container = ttk.Frame(self)
container.pack(fill='both', expand=True)
self.frames = {}
for frame in (Dashboard, PageOne, PageTwo, PageThree):
f = frame(container, self)
self.frames[frame] = f
f.grid(column=0, row=0, sticky="nswe")
self.switch_page(Dashboard)
def switch_page(self, frame):
f = self.frames[frame]
f.tkraise()
class Dashboard(ttk.Frame):
def __init__(self, master, controller):
super().__init__(master)
self.master = master
button1 = ttk.Button(self, text="Page One",
command=lambda: controller.switch_page(PageOne))
button1.grid(column=1, row=0, sticky="we")
button2 = ttk.Button(self, text="Page Two",
command=lambda: controller.switch_page(PageTwo))
button2.grid(column=1, row=1, sticky="we")
button3 = ttk.Button(self, text="Page Three",
command=lambda: controllerswitch_page(PageThree))
button3.grid(column=1, row=2, sticky="we")
ttk.Label(self).grid(column=0, row=0, rowspan=3)
ttk.Label(self).grid(column=2, row=0, rowspan=3)
class PageOne(ttk.Frame):
def __init__(self, master, controller):
super().__init__(master)
self.master = master
ttk.Button(self, text="Back to home",
command=lambda: controller.switch_page(Dashboard)
).pack()
class PageTwo(ttk.Frame):
def __init__(self, master, controller):
super().__init__(master)
self.master = master
ttk.Button(self, text="Back to home",
command=lambda: controller.switch_page(Dashboard)
).pack()
class PageThree(ttk.Frame):
def __init__(self, master, controller):
super().__init__(master)
self.master = master
ttk.Button(self, text="Back to home",
command=lambda: controller.switch_page(Dashboard)
).pack()
Also, I realized that I commented out the calls to grid.row_configure so I don't believe that is contributing to the issue. Again, the problem is not happening with the application in general, only when I click the fullscreen button on the window itself.

Related

Trouble Binding a Button in a Class - Python (Tkinter)

Just started learning Tkinter and was hoping someone could help me. I've been trying to bind a keyboard character (Enter button) to a tk button following this example and not getting anywhere.
Say I take the button (Enter) and try bind it nothing happens:
Enter.bind('<Return>', lambda:self.retrieve_Input(t))
If I bind to self instead using Lambda nothing happens also. I can get it to trigger if I remove the lambda but that's not the desired outcome
self.bind('<Return>', lambda:self.retrieve_Input(t))
My Code:
import sys
import tkinter as tk
from tkinter import ttk
class windows(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.wm_title("Test Application")
self.lift() #Bringing the GUI to the front of the screen
main_frame = tk.Frame(self, height=400, width=600) #Creating a main Frame for all pages
main_frame.pack(side="top", fill="both", expand=True)
main_frame.grid_rowconfigure(0, weight=1) #Configuring the location of the main frame using grid
main_frame.grid_columnconfigure(0, weight=1)
# We will now create a dictionary of frames
self.frames = {}
for F in (MainPage, CompletionScreen): #Add the page components to the dictionary.
page = F(main_frame, self)
self.frames[F] = page #The windows class acts as the root window for the frames.
page.grid(row=0, column=0, sticky="nsew")
self.show_page(MainPage) #Method to switch Pages
def show_page(self, cont):
frame = self.frames[cont]
frame.tkraise()
##########################################################################
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#switch_window_button = tk.Button(self, text="Go to the Side Page", command=lambda: controller.show_page(SidePage))
#switch_window_button.pack(side="bottom", fill=tk.X)
tk.Label(self, text="Project Python Search Engine", bg='white').pack()
tk.Label(self, text="", bg='white').pack()
tk.Label(self, text="Song", bg='white').pack()
tk.Label(self, text="", bg='white').pack()
t = tk.Entry(self, bg='white', width = 50)
t.pack()
tk.Label(self, text="", bg='white').pack()
Enter = tk.Button(self, text='Search', command= lambda:self.retrieve_Input(t))
Enter.pack()
tk.Button(self, text="Latest Popular Songs", command=lambda:self.Popular_Songs(t)).pack() #Line 210 onwards
Enter.bind('<Return>', lambda:self.retrieve_Input(t))
def retrieve_Input(self, t):
print ("work")
print (t)
class CompletionScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Completion Screen, we did it!")
label.pack(padx=10, pady=10)
switch_window_button = ttk.Button(
self, text="Return to menu", command=lambda: controller.show_page(MainPage)
)
switch_window_button.pack(side="bottom", fill=tk.X)
if __name__ == "__main__":
App = windows()
App.mainloop()
I'm not really sure what I'm missing
Answer: The button probably doesn't have the keyboard focus. When I run your code and then use the keyboard to move the focus to the button, your binding works. You probably want to bind to the entry widget rather than the button since that's what will have the keyboard focus. – Thanks Bryan Oakley

Destroying label frames

I am tinkering around with basic things in Tkinter/python. I am using the LabelFrame that corresponds with several buttons I click. I am struggling with trying to destroy the other frames and only displaying the frame I want visible when clicking on a certain button. When I run this code:
def lblframe1_view():
lblframe3.destroy()
lblframe2.destroy()
lblframe1 = LabelFrame(root, text="This is Frame One", height=370, width=370, bd=5, relief=FLAT).place(x=215, y=20)
btn1 = Button(lblframe1, text="this is button one").pack()
def lblframe2_view():
lblframe1.destroy()
lblframe3.destroy()
lblframe2 = LabelFrame(root, text="Frame Two", height=370, width=370, bd=5, relief=FLAT).place(x=215, y=20)
btn2 = Button(lblframe2, text="this is button two").pack()
def lblframe3_view():
lblframe1.destroy()
lblframe2.destroy
lblframe3 = LabelFrame(root, text="this is frame 3", height=370, width=370, bd=5, relief=FLAT).place(x=215, y=20)
def exit():
root.destroy()
manbtn1 = Button(root, text="Frame 1", bg="white", height=4, width=25, command=lblframe1).place(x=10, y=20)
mainbtn2 = Button(root, text="Frame 2, bg="white", height=4, width=25, command=lblframe2).place(x=10, y=120)
mainbtn3 = Button(root, text="Frame 3", bg="white", height=4, width=25, command=lblframe3).place(x=10, y=220)
exitbtn = Button(root, text="Exit", bg="white", height=4, width=25, command=exit).place(x=10, y=320)
When I run this I get the following error:
NameError: name 'lblframe3' is not defined
I have tried to put the functions after buttons and got the same error. I have done research on this
and haven't been able to figure out why this keeps happening. My hunch is python
Im not a experienced python programmer myself but there are a few things i noticed that might help you out.
Please make sure the code you post here is tested, it is a bit annoying if i have to debug the code before i can help you in the first place. (referring to the " in third line from the bottom as well as the indentation).
Button(command) takes a function as a argument. You provided a Object. So try to switch the command to command = lblframe1_view instead of lblframe1.
Frames in tkinter are objects, therefore they can not be accessed from a function where they were not defined in, unless u declare them global or pass it in as a argument. What that means is if lblframe1_view() is called, it does not know what lblframe3 or lblframe2 are.
I hope this helped you out.
EDIT:
I just realised that there might be one more problem with the structure of your programm that can not be solved by declaring the frames as globals.
In lblframe1_view u want to destroy an object that does not even exist yet, since it only will be created if lblframe2_view() and lblframe3_view() have been executed beforehand. But those two functions will also throw an error, since lblframe1 is not created yet.
I was able to do some research and found the answer I was looking for: I credit sentdex from Youtube for this. He basically stacks the frames on top of one another and a button click actually calls up the frame based on the command.
from tkinter import *
import tkinter as tk
from tkinter import ttk
LARGE_FONT = ("verdana", 12)
class tool(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="supporticon.ico")
self.geometry("400x400")
self.resizable(0,0)
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):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
frame.config(bg="darkblue")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
homeframe = LabelFrame(self, text="testing this out", width=200, height=350)
homeframe.config(background="darkblue", fg="white")
homeframe.pack()
label = Label(homeframe, text="Start Page", font=LARGE_FONT)
label.config(background="darkblue", fg="white", height=400, width=450)
label.place(x=10, y=20)
button1 = ttk.Button(homeframe, text="Vist Page 1",
command=lambda: controller.show_frame(PageOne))
button1.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
app = tool()
app.resizabe=(0,0)
app.geometry=("450x450")
app.mainloop()

Tkinter objects are not centering

I am trying to create a simple Python 3.x GUI project with tkinter, just to learn the language better (I started learning Python not long ago), of which the only thing it does is switching between different pages as a button is clicked. The problem is that the objects are not getting in the center of the screen. What's wrong in my code?
Images:
Start Page
First Page
Second Page
import tkinter as tk
LARGE_FONT = ("verdana", 10)
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.grid()
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="snew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage (tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label1 = tk.Label(self, text="Start Page", font=LARGE_FONT)
label1.grid(row=0, column=0)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
button1.grid(row=1, column=0)
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.grid(row=2, column=0)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label2 = tk.Label(self, text="Page One", font=LARGE_FONT)
label2.grid(row=0, column=0, sticky="E")
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.grid(row=1, column=0)
button3 = tk.Button(self, text="Go to Start Page",
command=lambda: controller.show_frame(StartPage))
button3.grid(row=2, column=0)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label2 = tk.Label(self, text="Page Two", font=LARGE_FONT)
label2.grid(row=0, column=0,sticky="E")
button2 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
button2.grid(row=1, column=0)
button3 = tk.Button(self, text="Go to Start Page",
command=lambda: controller.show_frame(StartPage))
button3.grid(row=2, column=0)
app = Application()
app.title("Application")
app.mainloop()
I put a menu on the left with tkinter. Everything to the right of the menu is aligned in the middle. How to align to the right of the menu
There is a text on the back of the textbox, but because it is center-aligned, it crosses over the text and the text does not appear.
Tkinter program
All code:
from tkinter import *
from tkinter import messagebox
import tkinter.font as font
import tkinter as tkMessageBox
from typing import Sized
from PIL import ImageTk, Image
window = Tk()
window.title("Ogretmen+")
window.geometry("900x447")
window.configure(bg="#202020")
genelgorunumbaslik = font.Font(size=20)
menuyazilar = font.Font(size=20)
guncellemebuyuk = font.Font(size=15)
def sinifYonetimi():
sinifyonetimiaciklama.config(text="Sınıf yönetimi kısmına hoş geldiniz. Bu bölümden sınıf ekleyebilir ve silebilirsiniz. Örnek sınıf adı: 10 B")
sinifyonetimiaciklama.grid(row=1, column=2)
sinifadigir.config(text="Sınıf Adı:")
sinifadigir.grid(row=2, column=2)
sinifadientry.grid(row=2, column=2)
islemsec.grid_remove()
sinifyonetimiaciklama = Label(fg="white", bg="#202020")
sinifadigir = Label(fg="white", bg="#202020")
sinifadientry = Entry(bd=4)
menubuton2 = Button(text="Sınıf Yönetimi", fg="white", bg="#282528", height=3, command=sinifYonetimi)
menubuton3 = Button(text="Öğrenci Yönetimi", fg="white", bg="#282528", height=3)
menubuton4 = Button(text="Konu Takibi", fg="white", bg="#282528", height=3)
menubuton5 = Button(text="İletişim", fg="white", bg="#282528", height=2)
menubuton2['font'] = menuyazilar
menubuton3['font'] = menuyazilar
menubuton4['font'] = menuyazilar
menubuton5['font'] = menuyazilar
menubuton2.grid(row=1, column=1, sticky="ew")
menubuton3.grid(row=2, column=1, sticky="ew")
menubuton4.grid(row=3, column=1, sticky="ew")
menubuton5.grid(row=4, column=1, sticky="ew")
islemsec = Label(text="Lütfen sol menüden işlem seçiniz.", bg="#202020", fg="white")
islemsec.grid(row=1, column=3)
window.mainloop()

New Tk window opens when changing background color

I have a GUI that contains several frames, each containing multiple labels/entry fields. I am trying to add a "Settings" option that will allow the user to change the background color of all frames & labels. So far I have managed to accomplish the task however with a caveat of a new Tk window popping up with the selected background instead of updating on the current window.
import tkinter as tk
from tkinter import ttk
from tkinter import colorchooser
bg_hex = '#f0f0f0f0f0f0' #default background color
def pick_color():
global bg_hex
bg_color = colorchooser.askcolor()
bg_hex = bg_color[1]
Master().update()
print(bg_hex)
class Master(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (HomePage, PageOne, PageTwo, Settings):
frame = F(container, self)
self.frames[F] = frame
frame.config(bg = bg_hex)
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(HomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text='Home Page', font=('Verdana', 12), bg=bg_hex)
label.pack(pady=5)
button1 = tk.Button(self, text='Page One', command=lambda: controller.show_frame(PageOne))
button1.pack(pady=5, ipadx=2)
button2 = tk.Button(self, text='Page Two', command=lambda: controller.show_frame(PageTwo))
button2.pack(pady=5)
button3 = tk.Button(self, text='Settings', command=lambda: controller.show_frame(Settings))
button3.pack(side='bottom', pady=10)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text='Page One', font='Verdana 14 bold underline', bg=bg_hex).grid(row=0, columnspan=2, pady=5)
button1 = tk.Button(self, text='Back to Home', command=lambda: controller.show_frame(HomePage))
button1.grid()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text='Page Two', font='Verdana 14 bold underline', bg=bg_hex).grid(row=0, columnspan=2,pady=5)
button1 = tk.Button(self, text='Back to Home', command=lambda: controller.show_frame(HomePage))
button1.grid()
class Settings(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text='Settings', font='Verdana 14 bold underline', bg=bg_hex).grid(row=0, columnspan=2,pady=5)
button1 = tk.Button(self, text='Back to Home', command=lambda: controller.show_frame(HomePage))
button1.grid()
button2 = tk.Button(self, text='Choose Background', command= pick_color)
button2.grid()
Master().mainloop()
No errors occur when running the the block of code, but when you select the "Choose Background" button and pick a color, a new Tk window opens with the selected background color instead of updating the current Tk window.
**Updating code to reflect no global variables in hopes that it helps someone else down the road.
I added self.controller = controller under each Frame class, combined pick_color and color_update into 1 function and placed under the Tk class.
def pick_color_bg(self):
bg_color = colorchooser.askcolor()
bg_hex = bg_color[1]
# Loop through pages and contained widgets and set color
for cls, obj in self.frames.items():
obj.config(bg=bg_hex) # Set frame bg color
for widget in obj.winfo_children():
if '!label' in str(widget):
widget.config(bg=bg_hex) # Set label bg color
Lastly, change the Button command to command=self.controller.pick_color_bg.
With these changes I was able to eliminate the need for global variables.
In the function pick_color() you create a new instance of Tk() which gets the new color:
Master().update() # Creates a new root window!
To change the color of the existing root window you will have to save a reference to it. Also you will have to write a function in the class Master() which updates the bg color. The color does not update automatically when you call update(), you have to configure the bg color for each frame.
some more
I'm having trouble reading your code without rewriting it quite a bit. You use the name Master for the class that instantiates the root window. I would call it Application or similar as the name master usually means a master as in "master and server" or maybe "parent". Also you use the name frame, which usually is the name of a frame, as the name for the different page class instances (HomePage, ... etc). This makes it difficult to read. It's like the word blue written with red letters.
I would rewrite it with names that are more descriptive which makes it easier to grasp. Then the problems will be easier to find and correct.
and even more
Took me a while but here is an example of how it could work with minor changes:
import tkinter as tk
from tkinter import ttk
from tkinter import colorchooser
bg_hex = '#f0f0f0f0f0f0' #default background color
def pick_color():
global bg_hex
bg_color = colorchooser.askcolor()
bg_hex = bg_color[1]
root.update_color() # Call function for updating color
print(bg_hex)
class Master(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (HomePage, PageOne, PageTwo, Settings):
frame = F(container, self)
self.frames[F] = frame
frame.config(bg = bg_hex)
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(HomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def update_color(self):
# Loop through pages and contained widgets and set color
for cls, obj in self.frames.items():
obj.config(bg=bg_hex) # Set frame bg color
for widget in obj.winfo_children():
if '!label' in str(widget):
widget.config(bg=bg_hex) # Set label bg color
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text='Home Page', font=('Verdana', 12), bg=bg_hex)
label.pack(pady=5)
button1 = tk.Button(self, text='Page One', command=lambda: controller.show_frame(PageOne))
button1.pack(pady=5, ipadx=2)
button2 = tk.Button(self, text='Page Two', command=lambda: controller.show_frame(PageTwo))
button2.pack(pady=5)
button3 = tk.Button(self, text='Settings', command=lambda: controller.show_frame(Settings))
button3.pack(side='bottom', pady=10)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text='Page One', font='Verdana 14 bold underline', bg=bg_hex).grid(row=0, columnspan=2, pady=5)
button1 = tk.Button(self, text='Back to Home', command=lambda: controller.show_frame(HomePage))
button1.grid()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text='Page Two', font='Verdana 14 bold underline', bg=bg_hex).grid(row=0, columnspan=2,pady=5)
button1 = tk.Button(self, text='Back to Home', command=lambda: controller.show_frame(HomePage))
button1.grid()
class Settings(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text='Settings', font='Verdana 14 bold underline', bg=bg_hex).grid(row=0, columnspan=2,pady=5)
button1 = tk.Button(self, text='Back to Home', command=lambda: controller.show_frame(HomePage))
button1.grid()
button2 = tk.Button(self, text='Choose Background', command= pick_color)
button2.grid()
root = Master() # Save a reference to the root window
root.mainloop()
I would recommend against changing color by means of a function in global scope. I think it would be better placed as a function of the Master() class. Then you would not have to use global variables.

Tkinter multiple frames/'pages': using canvas images

I would like to employ multiple frames in a GUI, where the page switches depending on the button clicked. I know that there's several threads already about this, and I've been looking at this one.
However, for my pages, I need different images on canvasses within each of my frames, so that when I raise a different frame, it comes with a new canvas and a new image on that canvas. I've tried a lot but I don't know how to get it to work so that the canvasses appear with their images.
Here's what I have so far, mostly copying from above link:
import tkinter as tk # python3
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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 = {}
self.frames["StartPage"] = StartPage(parent=container, controller=self)
self.frames["PageOne"] = PageOne(parent=container, controller=self)
self.frames["PageTwo"] = PageTwo(parent=container, controller=self)
self.frames["StartPage"].grid(row=0, column=0, sticky="nsew")
self.frames["PageOne"].grid(row=0, column=0, sticky="nsew")
self.frames["PageTwo"].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
self._canvas = tk.Canvas(parent, bg='white', width=900, height=3517, scrollregion=(0, 2800, 100, 800))
self._photo = tk.PhotoImage(file='images/homegraphic.gif')
self._canvas.create_image(0, 0, image=self._photo, anchor='nw')
label = tk.Label(self, text="This is the start page", font=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=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=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()
How do I get the canvas image to appear? I've spent a long time trying to figure this out and would appreciate any help!
The problem is here:
self._canvas = tk.Canvas(parent, ...)
Everything within a page needs to be a child of the page or one of its descendants.
It needs to be this:
self._canvas = tk.Canvas(self, ...)

Resources