I would like to position my title at the center of the window but it seems affect by the pack()
method of the button located at the bottom of the screen.
I remove the button at the bottom then the label is centralized. May I know how should i modify the code ?
# Import any required modules or libraries
from datetime import datetime
import tkinter as tk
class Window:
def __init__(self, root):
self.root = root
self.root.title("Software Title")
# Set window attributes
self.root.attributes("-fullscreen", True)
# Create a container frame
self.container = tk.Frame(self.root)
self.container.pack()
# Create buttons to switch between pages
self.button_home = tk.Button(self.root, text="Home",height= 3, width=13, command=self.show_home_page)
self.button_home.pack(side="left", anchor= "sw")
self.button_about = tk.Button(self.root, text="Page 2",height= 3, width=13, command=self.show_about_page)
self.button_about.pack(side="left", anchor= "sw")
self.button_contact = tk.Button(self.root, text="Page 3",height= 3, width=13, command=self.show_contact_page)
self.button_contact.pack(side="left", anchor= "sw")
# Create close button
self.close_button = tk.Button(self.root, text="Close",height= 3, width=13, command=self.close_window)
self.close_button.pack(side="right", anchor= "se")
# Create page frames
self.home_page_frame = tk.Frame(self.root)
self.about_page_frame = tk.Frame(self.root)
self.contact_page_frame = tk.Frame(self.root)
# Create labels within each page
self.home_page_label = tk.Label(self.home_page_frame, text="Main Page")
self.about_page_label = tk.Label(self.about_page_frame, text="Second Page")
self.contact_page_label = tk.Label(self.contact_page_frame, text="Third Page")
# Create buttons within each page
self.home_page_buttons = []
self.about_page_buttons = []
self.contact_page_buttons = []
# Page 1 content
home_page_button = tk.Button(self.home_page_frame, text="Home Page Button {i+1}")
self.home_page_buttons.append(home_page_button)
# Page 2 content
about_page_button = tk.Button(self.about_page_frame, text="About Page Button {i+1}")
self.about_page_buttons.append(about_page_button)
# Page 3 content
contact_page_button = tk.Button(self.contact_page_frame, text="Contact Page Button {i+1}")
self.contact_page_buttons.append(contact_page_button)
# Show the home page initially
self.show_home_page()
def close_window(self):
self.root.destroy()
def show_home_page(self):
# Hide other pages
self.about_page_frame.pack_forget()
self.contact_page_frame.pack_forget()
# Show the home page
self.home_page_frame.pack()
# Show the home page label
self.home_page_label.pack()
# Show the home page buttons
#for button in self.home_page_buttons:
# button.pack()
def show_about_page(self):
# Hide other pages
self.home_page_frame.pack_forget()
self.contact_page_frame.pack_forget()
# Show the about page
self.about_page_frame.pack()
# Show the about page label
self.about_page_label.pack()
# Show the about page buttons
#for button in self.about_page_buttons:
# button.pack()
# Hide the navigation buttons
#self.hide_navigation_buttons()
def show_contact_page(self):
# Hide other pages
self.home_page_frame.pack_forget()
self.about_page_frame.pack_forget()
# Show the contact page
self.contact_page_frame.pack()
# Show the contact page label
self.contact_page_label.pack()
# Show the contact page buttons
#for button in self.contact_page_buttons:
# button.pack()
# Hide the navigation buttons
#self.hide_navigation_buttons()
def hide_navigation_buttons(self):
# Hide the navigation buttons
self.button_home.pack_forget()
self.button_about.pack_forget()
self.button_contact.pack_forget()
# Other methods...
# Create the main window
root = tk.Tk()
# Create an instance of the Window class
window = Window(root)
# Run the main event loop
root.mainloop()
After modification, the buttons sticks at one side even i pack the close_button
button to right.
Code as below:
import tkinter as tk
class Window:
def __init__(self, root):
self.root = root
self.root.title("Software Title")
self.root.attributes("-fullscreen", True)
# Create page frames
self.navigator_frame = tk.Frame(self.root)
self.navigator_frame.pack(side='bottom',anchor='sw')
self.home_page_frame = tk.Frame(self.root, bg='wheat')
self.about_page_frame = tk.Frame(self.root, bg='tan')
self.contact_page_frame = tk.Frame(self.root, bg='tomato')
# Create buttons to switch between pages
self.button_home = tk.Button(self.navigator_frame, text="Home",height= 3, width=13,
command=self.show_home_page)
self.button_home.pack(side="left")
self.button_about = tk.Button(self.navigator_frame, text="Page 2",height= 3, width=13,
command=self.show_about_page)
self.button_about.pack(side="left")
self.button_contact = tk.Button(self.navigator_frame, text="Page 3",height= 3, width=13,
command=self.show_contact_page)
self.button_contact.pack(side="left")
# Create close button
self.close_button = tk.Button(self.navigator_frame, text="Close",height= 3, width=13,
command=self.close_window)
self.close_button.pack(side="right")
# Create labels within each page
self.home_page_label = tk.Label(self.home_page_frame, text="Main Page")
self.about_page_label = tk.Label(self.about_page_frame, text="Second Page")
self.contact_page_label = tk.Label(self.contact_page_frame, text="Third Page")
# Show the home page initially
self.show_home_page()
def show_home_page(self):
# Hide other pages
self.about_page_frame.pack_forget()
self.contact_page_frame.pack_forget()
self.home_page_frame.pack(expand=True, fill='both')
self.home_page_label.pack(expand=True)
def show_about_page(self):
# Hide other pages
self.home_page_frame.pack_forget()
self.contact_page_frame.pack_forget()
self.about_page_frame.pack(expand=True, fill='both')
self.about_page_label.pack(expand=True)
def show_contact_page(self):
# Hide other pages
self.home_page_frame.pack_forget()
self.about_page_frame.pack_forget()
self.contact_page_frame.pack(expand=True, fill='both')
self.contact_page_label.pack(expand=True)
def close_window(self):
self.root.destroy()
root = tk.Tk()
window = Window(root)
root.mainloop()