Tkinter: Create an homepage look like - python

I'd like to create for my GUI an homepage like look, I've manege to arrive at this point, but now I'm little stuck. What I'd like to achieve is change the bottom_box when clicking the button that link to Page One or Page Two.
Thanks for your help!
EDIT I've worked on it a little bit and I've arrived at this point. When running my code now I get this error :
"Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Python38-32\lib\tkinter_init_.py", line 1883, in call
return self.func(*args)
File "C:/folder/01.py", line 17, in
button1 = tk.Button(container, text="Go to Page One", command=lambda: controller.show_frame("PageOne"))
NameError: name 'controller' is not defined!
This is my edited code
import tkinter as tk
from PageOne import PageOne
from PageTwo import PageTwo
class MainApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
###MENU FRAME
left_frame = tk.Frame(root, borderwidth=1, bg= "white", relief="solid", highlightthickness=2)
left_frame.pack(side="left", expand=False, fill="y")
right_frame = tk.Frame(root, borderwidth=1, bg= "white", relief="solid", highlightthickness=2)
right_frame.pack(side="right", expand=True, fill="both")
container = tk.Frame(left_frame, borderwidth=1, bg= "white", relief="solid")
container.pack(expand=True, fill="both", padx=5, pady=5)
button1 = tk.Button(container, text="Go to Page One", command=lambda: controller.show_frame("PageOne"))
button1.pack(padx=20, pady=20)
button2 = tk.Button(container, text="Go to Page Two", command=lambda: controller.show_frame("PageTwo"))
button2.pack(padx=20, pady=20)
###TOP
top_box = tk.Frame(right_frame, borderwidth=1, bg= "white", relief="solid")
top_box.pack(expand=True, fill="both", padx=10, pady=10)
label_top = tk.Label(top_box, text="Title Logo", bg= "white")
label_top.pack()
###BOTTOM
bottom_box = tk.Frame(right_frame, borderwidth=1, bg= "green", relief="solid")
bottom_box.pack(expand=True, fill="both", padx=10, pady=10)
label_bott = tk.Label(bottom_box, text=" Logo", bg= "white")
self.frames = {}
for F in (PageOne, PageTwo):
page_name = F.__name__
frame = F(parent=bottom_box, 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):
frame = self.frames[page_name]
frame.tkraise()
if __name__ == "__main__":
root = tk.Tk()
root.geometry("1200x650")
MainApp(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Page 1
import tkinter as tk
from tkinter import font as tkfont
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label_pg1 = tk.Label(self, text="page 1", bg= "white")
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
Page 2
import tkinter as tk
from tkinter import font as tkfont
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label_pg2 = tk.Label(self, text="page 2", bg= "white")
if __name__ == "__main__":
app = SampleApp()
app.mainloop()

Related

Why tkinter multiple window GUI not working properly?

I am creating multiple window GUI using tkinter.
I did not receive any errors. First window runs successfully but when I clicked the button the second window doesn't run.
I don't understand where there's an error,
and if I pack Entry widget and Buttons they don't show in window.
import tkinter as tk
import pywhatkit
import pyautogui
class automation(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.geometry("842x510")
self.configure(bg="#ffffff")
self.resizable(False, False)
self.container = container = tk.Frame(self, background="#ffffff")
container.grid(row=0, column=0, sticky='nesw')
container.configure(background="#ffffff")
container.pack(side="top", fill="both")
self.frames = {}
for F in (MainWindow, SecondWindow):
page_name = F.__name__
frame = F(parent=container, controller=self)
frame.grid(row=0, column=0, sticky="nsew")
self.frames[page_name] = frame
self.show_frame('MainWindow')
def show_frame(self, page_name):
if page_name not in self.frames:
self.frames[page_name] = page_name(self.container, self)
frame = self.frames[page_name]
frame.tkraise()
def btn_clicked():
print("Button Clicked")
Code for first window.
class MainWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.frame = tk.Frame(self, background="#ffffff")
self.canvas = tk.Canvas(self, bg="#ffffff", height=510, width=391, bd=0,
highlightthickness=0, relief="ridge")
self.canvas.place(x=0, y=0)
self.canvas.pack(side='left')
self.background_img = tk.PhotoImage(file="background.png")
self.canvas.create_image(0, 0, image=self.background_img, anchor="nw")
self.img1 = tk.PhotoImage(file="img1.png")
self.b1 = tk.Button(self, image=self.img1, borderwidth=0, highlightthickness=0,
command=lambda: controller.show_frame(SecondWindow),
relief="flat")
self.b1.place(x=392, y=100, width=348, height=62)
self.b1.pack(padx=10, pady=125)
self.b1.pack()
# creating third button
self.img3 = tk.PhotoImage(file="img3.png")
self.b3 = tk.Button(self, image=self.img3, borderwidth=0, highlightthickness=0,
command=btn_clicked, relief="flat")
self.b3.place(x=392, y=200, width=348, height=62)
self.b3.pack(padx=10, pady=0)
self.b3.pack()
self.canvas.pack()
def send(num, msg, hour, minute):
pywhatkit.sendwhatmsg(f"+91{num}", msg, hour, minute, 36)
print("hello")
pyautogui.click()
pyautogui.press("enter")
Code for second window:
class SecondWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
a = tk.StringVar()
b = tk.StringVar()
c = tk.IntVar()
d = tk.IntVar()
self.controller = controller
self.frame = tk.Frame(self, background="#ffffff")
self.canvas = tk.Canvas(self, bg="#ffffff", height=510, width=842, bd=0,
highlightthickness=0, relief="ridge")
self.canvas.place(x=0, y=0)
self.canvas.pack(side='left')
# set image in background
self.background_img = tk.PhotoImage(file="background2.png")
self.canvas.create_image(0, 0, image=self.background_img, anchor="nw")
# enter a number
self.entry1_img = tk.PhotoImage(file="textBox1.png")
self.canvas.create_image(570, 112, image=self.entry1_img)
self.entry1 = tk.Entry(self, bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=a)
self.entry1.place(x=423, y=90, width=280, height=45)
#self.entry1.pack()
# enter a massage
self.entry2_img = tk.PhotoImage(file="textBox2.png")
self.canvas.create_image(570, 225, image=self.entry2_img)
self.entry2 = tk.Entry(self, bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=b)
self.entry2.place(x=423, y=203, width=280, height=45)
#self.entry2.pack()
# enter a time -> Hour
self.entry3_img = tk.PhotoImage(file="textBox3.png")
self.canvas.create_image(470, 329, image=self.entry3_img)
self.entry3 = tk.Entry(self, bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=c)
self.entry3.place(x=423, y=312, width=80.0, height=35)
#self.entry3.pack()
# minute
self.entry4_img = tk.PhotoImage(file="textBox4.png")
self.canvas.create_image(676, 329, image=self.entry4_img)
self.entry4 = tk.Entry(self, bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=d)
self.entry4.place(x=630, y=312, width=80.0, height=35)
#self.entry4.pack()
# Go home
self.img4 = tk.PhotoImage(file="img4.png")
self.b4 = tk.Button(self, image=self.img4, borderwidth=0, highlightthickness=0,
command=lambda: controller.show_frame(MainWindow), relief="flat")
self.b4.place(x=418, y=400, width=100, height=37)
#self.b4.pack()
# Send message
self.img5 = tk.PhotoImage(file="img5.png")
self.b5 = tk.Button(self, image=self.img5, borderwidth=0, highlightthickness=0,
command=lambda: send(a.get(), b.get(), c.get(), d.get()),
relief="flat")
self.b5.place(x=642, y=400, width=100, height=37)
#self.b5.pack()
self.canvas.pack()
if __name__ == '__main__':
app = automation()
app.mainloop()
command=lambda: controller.show_frame(SecondWindow), is passing the class id as argument so page_name is not being found.
suggest
command=lambda: controller.show_frame('SecondWindow')

AttributeError: 'LoginPage' object has no attribute 'menubar'

I am trying to create a web app with different pages. Till now I have created a log in page and home page. I have created each page as a frame. When I log in it directs me to home page. Until this the db and UI works well.
Now I would like to have different menubar for each frame.
My HomePage should have menubar with options 'Home' and 'Careers'.
To implement this I created a function for menbar in HomePage. I took the reference from Tkinter add menu bar in Frames
The code I executed is as given below
import tkinter as tk
from tkinter import ttk
import mysql.connector
#from tkinter import messagebox
class App(tk.Tk):
bg_img_path = "images\\bg9.png"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("1500x750")
main_frame = tk.Frame(self, width=200, height=50, highlightbackground="black", highlightthickness=1,
background="#e6ffe6")
main_frame.pack(side='top', fill='both', expand='True')
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
self.bkgr_image = tk.PhotoImage(file=self.bg_img_path)
self.frames = {}
for F in (LoginPage,HomePage):
page_name = F.__name__
frame = F(main_frame, self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame("LoginPage")
def show_frame(self, container):
frame = self.frames[container]
frame.tkraise()
menubar = frame.menubar(self)
self.configure(menu=menubar)
class BasePage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.root = parent
label_bkgr = tk.Label(self, image=controller.bkgr_image)
label_bkgr.place(x=0, y=0)
class LoginPage(BasePage):
def __init__(self, parent, controller):
super().__init__(parent, controller)
login_frame = tk.Frame(self, width=200, height=50,background="white")
login_frame.grid(row=400, column=20, padx=500, pady=250)
self.label_title = tk.Label(login_frame, text="Log In", font=("Helvetica", 40), bg="white")
self.label_title.grid(row=0, column=20, padx=10, pady=10)
self.label_username = tk.Label(login_frame, text="Username", font=("Helvetica", 20), bg="white")
self.label_username.grid(row=50, column=20, padx=10, pady=10)
self.entry_username = tk.Entry(login_frame, width=15, font=("Helvetica", 20),bd = 3)
self.entry_username.grid(row=50, column=30, padx=10, pady=10)
self.label_password = tk.Label(login_frame, text="Password", font=("Helvetica", 20), bg="white")
self.label_password.grid(row=60, column=20, padx=10, pady=10)
self.entry_password = tk.Entry(login_frame, width=15, font=("Helvetica", 20),bd = 3)
self.entry_password.grid(row=60, column=30, padx=10, pady=10)
self.login_button = tk.Button(login_frame, text="Log In", command=lambda: [self.submit(),self.controller.show_frame("HomePage")], font=("Helvetica", 20),
bg="white")
self.login_button.grid(row=70, column=25, padx=10, pady=10)
def submit(self):
self.u_name = self.entry_username.get()
self.p_word = self.entry_password.get()
employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
cursor_variable = employee.cursor()
cursor_variable.execute("INSERT INTO login VALUES ('" + self.u_name + "','" + self.p_word + "')")
employee.commit()
employee.close()
#messagebox.showinfo("Log In", "Succesfull")
class HomePage(BasePage):
def __init__(self, parent, controller):
super().__init__(parent, controller)
label1 = ttk.Label(self, text='Home', font=("Helvetica", 20))
label1.pack(padx=10, pady=10)
def menubar(self):
menubar = tk.Menu(self.master)
pageMenu = tk.Menu(menubar)
pageMenu.add_command(label="Home")
pageMenu.add_command(label= "Careers")
return menubar
app = App()
app.mainloop()
The error I am getting is as follows:
C:\Users\write\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/write/PycharmProjects/OOP trials/login_bg_img_trial2.py"
Traceback (most recent call last):
File "C:\Users\write\PycharmProjects\OOP trials\login_bg_img_trial2.py", line 107, in <module>
app = App()
File "C:\Users\write\PycharmProjects\OOP trials\login_bg_img_trial2.py", line 30, in __init__
self.show_frame("LoginPage")
File "C:\Users\write\PycharmProjects\OOP trials\login_bg_img_trial2.py", line 35, in show_frame
menubar = frame.menubar(self)
AttributeError: 'LoginPage' object has no attribute 'menubar'
Can anyone tell why does it shows attribute error? I am not able to understand the concept.

TypeError: __init__() missing 1 required positional argument: 'parent'

I tried to create an exit button to close Tkinter application. So far everything works as planned, but if I press on "EXIT" button, I receive an error. I don't know, how can I improve that. I tried to use function way to solve this problem, but it doesn't work as well (Unambiguously by my mistake). Can anyone advise how best to integrate this button?
My code:
from tkinter import *
import tkinter.ttk as ttk
class CollegeApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
for F in (StartPage, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.startMenu()
def startMenu(self):
heading = Label(self, text="College Tournament Points\n Count Software",
font=('Arial', 25))
heading.grid(row=0, column=0, columnspan=2, padx=240, pady=40)
start_Btn = Button(self, text="Start", font="Arial 16", width=8,
command=lambda: self.controller.show_frame(PageTwo))
start_Btn.grid(row=1, column=0, padx=30)
exit_Btn = Button(self, text="EXIT", font="Arial 16", width=8,
command=exitButton)
exit_Btn.grid(row=1, column=1, padx=30)
def starting_Program():
pass
class exitButton(Button):
def __init__(self, parent):
Button.__init__(self, parent)
self[Button] = parent.destroy
self.pack(BOTTOM)
class PageTwo(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.make_widget()
def make_widget(self):
ttk.Label(self, text='This is page two').grid(padx=(20, 20), pady=(20, 20))
button1 = ttk.Button(self, text='Previous Page',
command=lambda: self.controller.show_frame(StartPage))
button1.grid()
if __name__ == '__main__':
app = CollegeApp()
app.geometry("800x500")
app.title('Points Counter')
app.mainloop()
Error:
/Users/aleks/PycharmProjects/PointCounter/venv/bin/python /Users/aleks/PycharmProjects/PointCounter/main.py
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
TypeError: __init__() missing 1 required positional argument: 'parent'
Process finished with exit code 0
try doing this
from tkinter import *
import tkinter.ttk as ttk
class CollegeApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
for F in (StartPage, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.startMenu()
def startMenu(self):
heading = Label(self, text="College Tournament Points\n Count Software",
font=('Arial', 25))
heading.grid(row=0, column=0, columnspan=2, padx=240, pady=40)
start_Btn = Button(self, text="Start", font="Arial 16", width=8,
command=lambda: self.controller.show_frame(PageTwo))
start_Btn.grid(row=1, column=0, padx=30)
exit_Btn = Button(self, text="EXIT", font="Arial 16", width=8,
command=self.controller.destroy)
exit_Btn.grid(row=1, column=1, padx=30)
def starting_Program():
pass
class PageTwo(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.make_widget()
def make_widget(self):
ttk.Label(self, text='This is page two').grid(padx=(20, 20), pady=(20, 20))
button1 = ttk.Button(self, text='Previous Page',
command=lambda: self.controller.show_frame(StartPage))
button1.grid()
if __name__ == '__main__':
app = CollegeApp()
app.geometry("800x500")
app.title('Points Counter')
app.mainloop()

How do I enable .grid?

I wanted to position in the username and password such that it is next to each other. In order to achieve that I used the grid method but I keep getting an error that says:
_tkinter.TclError: cannot use geometry manager grid inside .!frame.!adminlogin.!frame which already has slaves managed by pack.
Is there a way I can let it use even grid?
import tkinter as tk
from tkinter import font as tkfont
import PIL.Image
from PIL import ImageTk
from tkinter import *
import tkinter.font as font
import sqlite3, hashlib
from datetime import date
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")
# 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
self.geometry ("1024x768")
container = tk.Frame(self)
container.pack(fill="both", side= 'top',expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, StudentLogin, AdminLogin):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
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
frame = tk.Frame(self, width = 1024, height = 768, bg="teal")
frame.pack(fill="both", expand=True, side="top")
label = tk.Label(frame, text="WELCOME", font=controller.title_font)
label.pack(side="top", fill="x", pady=50)
button1 = tk.Button(frame, text="Admin Login",
command=lambda: controller.show_frame("AdminLogin"), width = 50, height=10, font=30, bg='powder blue')
button2 = tk.Button(frame, text="Student Login",
command=lambda: controller.show_frame("StudentLogin"), width=50, height=10, font=30, bg='powder blue')
button1.pack(pady=10)
button2.pack(pady=10)
class AdminLogin(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
frame = tk.Frame(self, width = 1024, height = 768, bg='teal')
frame.pack(fill="both", side ="top", expand=True)
label = tk.Label(frame, text="Enter username and password to login", font=controller.title_font)
label.pack(side="top", fill="x", pady=30, padx =10)
userLabel = tk.Label(frame, text = "Enter Username: ", font=50)
userLabel.pack(padx=10, pady=10)
userEntry = tk.Entry(frame)
userEntry.pack(padx=10, pady=10)
passwordL = tk.Label(frame, text = "Enter Password: ", font=50)
passwordL.pack(padx=10, pady=10)
passEntry = tk.Entry(frame, show="*")
passEntry.pack(padx=10, pady=10)
button = tk.Button(frame, text="Back",
command=lambda: controller.show_frame("StartPage"), bg='powder blue')
button.pack()
class StudentLogin(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
frame = tk.Frame(self, width = 1024, height = 768, bg='teal')
frame.pack(fill="both", side ="top", expand=True)
label = tk.Label(frame, text="Enter username and password to login", font=controller.title_font)
label.pack(side="top", fill="x", pady=30, padx =10)
button = tk.Button(frame, text="Back",
command=lambda: controller.show_frame("StartPage"), bg='powder blue')
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
Unfortunately you cannot mix both the pack and grid methods. You have to only pick one, which you will use throughout that master. Pack is easier to use, but with grid you have more control over where your widgets are going to be displayed in the window. (I would have done this as a comment to your post, not an answer, but i do not have enough reputation to do so) I hope this helps :)

My buttons sit side by side in my Home window but they appear in the middle. How do I get them higher up?

I am having a similar issue with the Shop class too. My first button (Teapops) is where I want all my buttons on my Home window and Shop window to be (except for Back to Home) if I use:
button1.pack(side=TOP, anchor=NW, padx=10, pady=60, expand=NO)
button2.pack(side=TOP, anchor=NW, padx=30, pady=60, expand=NO)
button3.pack(side=TOP, anchor=NW, padx=60, pady=60, expand=NO)
But then all the others appear lower and lower and I don't have any idea why except maybe I have an issue with my frames?
If I use this,
button1.pack(side=LEFT, anchor=NW, fill=BOTH, expand=1)
button2.pack(side=LEFT, anchor=NW, fill=BOTH, expand=1)
button3.pack(side=LEFT, anchor=NW, fill=BOTH, expand=1)
then all my buttons appear side by side but in the middle of the screen again like on my Home screen:
Can someone please explain to me whats going on? I think there are some basics about Frames that I am not understanding. Please help!!!!
import Tkinter as tk
from Tkinter import *
TITLE_FONT = (“Helvetica”, 18, “bold”)
CREDITS_FONT = (“Helvetica”, 12, “bold”)
class App(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 (Home, My_Plnts, Jrnl, Shop, Mail):
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(“Home”)
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class Home(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
Home.configure(self, background=’#ade5ad’)
label = tk.Label(self, text=“Welcome Home, Maebert!”, background=’#ade5ad’, font=TITLE_FONT)
label.pack(side=“top”, fill=“x”, pady=10)
button1 = tk.Button(self, text=“My Plnts”,
command=lambda: controller.show_frame(“My_Plnts”))
button2 = tk.Button(self, text=“Jrnl”,
command=lambda: controller.show_frame(“Jrnl”))
button3 = tk.Button(self, text=“Shop”,
command=lambda: controller.show_frame(“Shop”))
button4 = tk.Button(self, text=“Mail”,
command=lambda: controller.show_frame(“Mail”))
button1.pack(side=LEFT, padx=60)
button2.pack(side=LEFT, padx=60)
button3.pack(side=LEFT, padx=60)
button4.pack(side=LEFT, padx=60)
class My_Plnts(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
My_Plnts.configure(self, background=’#ade5ad’)
label = tk.Label(self, text=“My Plnts”, background=’#ade5ad’, font=TITLE_FONT)
label.pack(side=“top”, anchor=NW, fill=“x”, pady=10)
button = tk.Button(self, text=“Back to Home”,
command=lambda: controller.show_frame(“Home”))
button.pack(side=“top”, anchor=NE)
class Jrnl(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
Jrnl.configure(self, background=’#ade5ad’)
label = tk.Label(self, text=“Jrnl”, background=’#ade5ad’, font=TITLE_FONT)
label.pack(side=“top”, fill=“x”, pady=10)
button = tk.Button(self, text=“Back to Home”,
command=lambda: controller.show_frame(“Home”))
button.pack(side=“top”, anchor=NE)
class Shop(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
Shop.configure(self, background=’#ade5ad’)
label = tk.Label(self, text=“Shop”, background=’#ade5ad’, font=TITLE_FONT)
label.pack(side=“top”, fill=“x”, pady=10)
label = tk.Label(self, text=“More at www.gfc.com”, background=’#ade5ad’, font=CREDITS_FONT)
label.pack(side=“bottom”, fill=“x”, pady=10)
button = tk.Button(self, text=“Back to Home”,
command=lambda: controller.show_frame(“Home”))
button1 = tk.Button(self, text=“Teapops”,
command=lambda: controller.show_frame(“Teapops”))
button2 = tk.Button(self, text=“Plants”,
command=lambda: controller.show_frame(“Plants”))
button3 = tk.Button(self, text=“Nail Polish”,
command=lambda: controller.show_frame(“Nail_Polish”))
button.pack(side=“top”, anchor=NE)
button1.pack(side=LEFT, anchor=NW, fill=BOTH, expand=1)
button2.pack(side=LEFT, anchor=NW, fill=BOTH, expand=1)
button3.pack(side=LEFT, anchor=NW, fill=BOTH, expand=1)
“”“
button1.pack(side=TOP, anchor=NW, padx=10, pady=60, expand=NO)
button2.pack(side=TOP, anchor=NW, padx=30, pady=60, expand=NO)
button3.pack(side=TOP, anchor=NW, padx=60, pady=60, expand=NO)
”“”
class Mail(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
Mail.configure(self, background=’#ade5ad’)
label = tk.Label(self, text=“Mail”, background=’#ade5ad’, font=TITLE_FONT)
label.pack(side=“top”, fill=“x”, pady=10)
button = tk.Button(self, text=“Back to Home”,
command=lambda: controller.show_frame(“Home”))
button.pack(side=“top”, anchor=NE)
if __name__ == “__main__”:
app = App()
app.minsize(300,300)
app.geometry(“800x800”)
app.mainloop()
I built the below program to demonstrate to you how the .pack() method works, please feel free to play around with the different options and see how each affects the output:
from tkinter import *
root = Tk()
top = Toplevel()
top.withdraw()
var1 = StringVar(root)
var1.set("top")
var2 = StringVar(root)
var2.set("none")
var4 = StringVar(root)
var4.set("center")
var3 = BooleanVar(root)
def command(top, var1, var3, var2):
top.destroy()
top = Toplevel()
top.geometry("500x500")
Label(top, text="Welcome home").pack()
Button(top, text="Button1").pack(side=var1.get(), fill=var2.get(), expand=var3.get(), anchor=var4.get())
Button(top, text="Button2").pack(side=var1.get(), fill=var2.get(), expand=var3.get(), anchor=var4.get())
Button(top, text="Button3").pack(side=var1.get(), fill=var2.get(), expand=var3.get(), anchor=var4.get())
Button(top, text="Button4").pack(side=var1.get(), fill=var2.get(), expand=var3.get(), anchor=var4.get())
option1 = OptionMenu(root, var1, "top", "left", "bottom", "right")
check1 = Checkbutton(root, variable=var3, text="Expand?")
option2 = OptionMenu(root, var2, "none", "x", "y", "both")
option3 = OptionMenu(root, var4, "center", "n", "ne", "e", "se", "s", "sw", "w", "nw")
button1 = Button(root, text="Render", command=lambda:command(top, var1, var3, var2))
option1.pack()
check1.pack()
option2.pack()
option3.pack()
button1.pack()
root.mainloop()
This should show you how the different options affect the results of the .pack().
More to the point I believe the effect you are looking for can be achieved using .pack(side="left", expand="true", fill="x", anchor="n").

Categories