-3

For my current Python project, I usually work at college on an x sized monitor, however when I want to work on it at home I have a much smaller screen. The majority of my widgets are currently sized with height and width parameters, and so when I work at home, my Tkinter frames that had been adjusted to fit my work monitor are then too big for the home screen, and only about two-thirds of my frame content then appears on the screen. I am using grid geometry.

There are also issues even when widgets don't have height parameters, for example, as there are simply too many rows of widgets to fit on my smaller screen.

Is it possible to scale Tkinter frames at all, so that when I come home to work on my project my window proportionately reduces in size to fit my smaller screen?

An example class is below:

class home_page(tk.Frame):

    def __init__(self, parent, controller):
        
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background="white")
        h_title = tk.Label(self, text="Welcome to Fantasy Football!", fg="white", height=2, width=60, anchor="center", bg="#0ebf08", font=controller.title_font)
        h_squad_hub_lb = tk.Label(self, text="Squad Hub", fg="white", width=13, anchor="center", bg="#38003c", font=controller.title_font)
        squad_hub_text = "Selection headaches keeping you up\nat night? View your team below: "
        h_sh_text = tk.Label(self, text=squad_hub_text, bg="white", font=("Segoe UI", 13))
        h_view_team_bt = tk.Button(self, text="View Team", bg="#38003c", fg="white", command=lambda: controller.show_frame("pitch_view_team_page"))
        h_transfers_bt = tk.Button(self, text="Transfers", bg="#38003c", fg="white", command=lambda: controller.show_frame("pitch_view_transfers_page"))
        self.sh_img = PhotoImage(file = "Miscellaneous images/Squad Hub.png")
        h_sh_img = tk.Label(self, image=self.sh_img)
        h_rules_lb = tk.Label(self, text="Rules", fg="white", bg="#38003c", width=13, anchor="center", font=controller.title_font)
        rules_text = "Need to know the game before you can\ncrush it? Check out our rules below: "
        h_r_text = tk.Label(self, text=rules_text, bg="white", font=("Segoe UI", 13))
        h_selection_info_bt = tk.Button(self, text="Selection Info", bg="#38003c", fg="white", command=lambda: controller.show_frame("seli_rules_page"))
        h_scoring_info_bt = tk.Button(self, text="Scoring Info", bg="#38003c", fg="white", command=lambda: controller.show_frame("scoi_rules_page"))
        self.r_img = PhotoImage(file = "Miscellaneous images/Rules.png")
        h_r_img = tk.Label(self, image=self.r_img)
        h_stats_lb = tk.Label(self, text="Statistics", fg="white", bg="#38003c", width=13, anchor="center", font=controller.title_font)
        stats_text= "Want some hard facts to help decide on\nyour transfers? Check out the stats below: "
        h_s_text = tk.Label(self, text=stats_text, bg="white", font=("Segoe UI", 13))
        h_table_bt = tk.Button(self, text="League Table", bg="#38003c", fg="white", command=lambda: controller.show_frame("lgetbl_stats_page"))
        h_plrsts_bt = tk.Button(self, text="Player Stats", bg="#38003c", fg="white", command=lambda: controller.show_frame("plrsts_stats_page"))
        self.s_img = PhotoImage(file = "Miscellaneous images/Stats.png")
        h_s_img = tk.Label(self, image=self.s_img)
        h_news_lb = tk.Label(self, text="News", fg="white", width=13, anchor="center", bg="#38003c", font=controller.title_font)
        news_text = "Want to hear what your favourite team is\nup to? Check out club news below: "
        h_n_text = tk.Label(self, text=news_text, bg="white", font=("Segoe UI", 13))
        h_club_list_bt = tk.Button(self, text="Club List", bg="#38003c", fg="white", command=lambda: controller.show_frame("clli_stats_page"))
        h_preferences_bt = tk.Button(self, text="My Preferences", bg="#38003c", fg="white", command=lambda: controller.show_frame("mpref_settings_page"))
        self.n_img = PhotoImage(file = "Miscellaneous images/News.png")
        h_n_img = tk.Label(self, image=self.n_img)

        h_title.grid(row=0, column=0, columnspan=15, pady=(60,60))
        
        h_squad_hub_lb.grid(row=1, column=0, columnspan=3)
        h_sh_text.grid(row=2, column=0, columnspan=3, rowspan=3)
        h_sh_img.grid(row=2, column=3, rowspan=2)
        h_view_team_bt.grid(row=4, column=0, pady=(20,40))
        h_transfers_bt.grid(row=4, column=1, pady=(20,40))
        
        h_rules_lb.grid(row=1, column=11, columnspan=3)
        h_r_text.grid(row=2, column=11, columnspan=3, rowspan=2)
        h_r_img.grid(row=2, column=14, rowspan=2)
        h_selection_info_bt.grid(row=4, column=11, pady=(20,40))
        h_scoring_info_bt.grid(row=4, column=12, pady=(20,40))
        
        h_stats_lb.grid(row=5, column=0, columnspan=3)
        h_s_text.grid(row=6, column=0, columnspan=3, rowspan=2)
        h_s_img.grid(row=6, column=3, rowspan=2)
        h_table_bt.grid(row=8, column=0, pady=(20,40))
        h_plrsts_bt.grid(row=8, column=1, pady=(20,40))
        
        h_news_lb.grid(row=5, column=11, columnspan=3)
        h_n_text.grid(row=6, column=11, columnspan=3, rowspan=2)
        h_n_img.grid(row=6, column=14, rowspan=2)
        h_club_list_bt.grid(row=8, column=11, pady=(20,40))
        h_preferences_bt.grid(row=8, column=12, pady=(20,40)) 
  • 1
    have you looked at `.winfo_screenwidth()` and `.winfo_screenheight()`? also use `pack` or `grid` not `place` – Matiiss Nov 25 '21 at 18:11
  • @Matiiss apologies, I have now edited to state I'm using grid geometry, I believe I would be able to scale using packing but grid is far more practical for this project. I will check out the .winfo commands, thank you – JRandall26 Nov 25 '21 at 18:18
  • In addition to `.winfo_screenwidth() `and `.winfo_screenheight()`, there's `winfo_fpixels()` and `winfo_fpixels()` that will convert physical units like inches and centimeters into pixels. The returned values can be useful when using the `place` layout manager as well as when specifing the size (width or height) of widgets, like `width=10c` for 10 centimeters. – martineau Nov 25 '21 at 18:45
  • Please provide code examples what you made so far, so it is posible for us to help you properly. – Thyrus Nov 26 '21 at 11:06
  • We can't run the code you posted, for several reasons. Please try to provide an example that doesn't use images, or uses blank images that don't rely on external files (eg: `PhotoImage(width=32, height=32)`). The text of your question says you use width and height for most widgets, but only one widget here has a height. Also, please describe just how small you need to make the UI. When I removed the images this code is less than 700x500. – Bryan Oakley Nov 26 '21 at 20:21

1 Answers1

1

As a general rule of thumb, you should only give widths and heights to widgets where you really do care about widths and heights, such as with a text widget, listbox, or treeview. You usually shouldn't need to give a size to frames or the window as a whole.

When you do assign sizes, you should pick the minimum size necessary and then use pack, place, or grid options that allow the widgets to be resized when the window grows or shrinks.

Without more specifics about your particular problem it will be impossible for us to give more specific advice.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685