Search code examples
pythontkinter

How can I scale my whole page with the size of the window?


This is the code that I am using to learn:

import tkinter as tk


root = tk.Tk()


all = tk.Label(root)
welcome = tk.Label(all, text="Welcome", font=("Verdana", 50), anchor="center")
welcome.grid(row=0, column=0, padx=10, pady=10)
welcome2 = tk.Label(all, text="To my software", font=("Verdana", 30), anchor="center")
welcome2.grid(row=1, column=0, padx=10, pady=10)
start = tk.Button(all, text="Start", width=15, height=1, font=("Verdana", 12))
start.grid(row=2, column=0, padx=10, pady=10)
all.place(relx=0.5, rely=0.5, anchor=tk.CENTER)


root.mainloop()

How can I make the size of this all scale up and down dependent on the size of the tkinter window?

This differs from TkInter Label Change Font Size by Text Length because I would like to also scale the Button and the Entry boxes that are also in the parent "all" label. Sort of like zooming in and out of it all.


Solution

  • From what I have found, the easiest way to do this would be to make it look good on one resolution and then have a function scale the font relative to the resolution. Like this:

    import tkinter as tk
    
    
    root = tk.Tk()
    
    def new_size(size):
        change = root.winfo_screenwidth()/ 1920
        print(change)
        return int(size * change)
    
    
    main = tk.Frame(root)
    welcome = tk.Label(main, text="Welcome", font=("Verdana", new_size(50)), anchor="center")
    welcome.grid(row=0, column=0, padx=10, pady=10)
    welcome2 = tk.Label(main, text="To my software", font=("Verdana", new_size(30)), anchor="center")
    welcome2.grid(row=1, column=0, padx=10, pady=10)
    start = tk.Button(main, text="Start", width=15, height=1, font=("Verdana", new_size(12)))
    start.grid(row=2, column=0, padx=10, pady=10)
    main.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
    
    root.attributes("-fullscreen", True)
    root.mainloop()