I'm really new to tkinter and I want to experiment with using multiple windows in tkinter. I have borrowed the code from here: https://pythonprogramming.altervista.org/create-more-windows-with-tkinter/?doing_wp_cron=1645372524.8916330337524414062500 I am looking to be able to move the buttons from being just centred in the middle and at the top of the window but I am unsure of how to move the buttons in the window. I know how to move buttons normally outside of a class using pack() but I am unsure about how to do it within the class and this code. When I try to add attributes like side or fill, the window would appear but the button wouldn't. I have looked everywhere for a solution but I haven't found anything that suits my problem
Code:
import tkinter as tk
class Win1:
def __init__(self, master):
self.master = master
self.master.geometry("800x800")
self.frame = tk.Frame(self.master)
self.butnew("Click to open Window 2", "2", Win2)
self.butnew("Click to open Window 3", "3", Win3)
self.frame.pack()
def butnew(self, text, number, _class):
tk.Button(self.frame, text = text, command= lambda: self.new_window(number, _class)).pack()
def new_window(self, number, _class):
self.new = tk.Toplevel(self.master)
_class(self.new, number)
class Win2:
def __init__(self, master, number):
self.master = master
self.master.geometry("800x800 200 200")
self.frame = tk.Frame(self.master)
self.quit = tk.Button(self.frame, text = f"Quit this window n. {number}", command = self.close_window)
self.quit.pack()
self.frame.pack()
def close_window(self):
self.master.destroy()
class Win3:
def __init__(self, master, number):
self.master = master
self.master.geometry("800x800 200 200")
self.frame = tk.Frame(self.master)
self.quit = tk.Button(self.frame, text = f"Quit this window n. {number}", command = self.close_window)
self.quit.pack()
self.label = tk.Label(self.frame, text="THIS IS ONLY IN THE THIRD WINDOW")
self.label.pack()
self.frame.pack()
def close_window(self):
self.master.destroy()
root = tk.Tk()
app = Win1(root)
root.mainloop()
CodePudding user response:
I recommend using .grid() instead of .pack() as it gives you more control of how to place your widget. With grid() it gives you the option to set the column, row, padx and pady, the sticky value as well as others.
Here's an exemple of using grid to render a button onto the screen:
Button = Button(root, text="Button")
Button.grid(row=1, column=1)
To center the button onto the screen while using grid() you add the "sticky" option.
Here's an exemple of the stick option in action:
Button = Button(root, text="Button")
Button.grid(row=1, column=1, sticky=N)
When using sticky here are all your options:
- N North or Top Center
- S South or Bottom Center
- E East or Right Center
- W West or Left Center
- NW North West or Top Left
- NE North East or Top Right
- SE South East or Bottom Right
- SW South West or Bottom Left
- NS stretches the widget vertically. However, it leaves the widget centered horizontally.
- EW stretches the widget horizontally. However, it leaves the
widget centered vertically.
Here's a link to a great resource where you can learn more about using grid():