John
John

Reputation: 222

Sizing Widgets to Work in Any Sized Screen in Tkinter

I'm an amateur programmer and I used Gtk a long time ago and PyQt recently to create GUIs but have just returned to Tkinter for it's elegance. In Gtk and Qt the grid commands seem to resize widgets in an automatic fashion for the window size. In Tkinter I'm really confused by how to setup a window to be resizable. Using Brian Oakley's suggestion at: How can i fit my tkinter app to any size screen? and the various sources cited I've tried to write the following simple code so it should work on any size screen. For some reason I am unable change the size of the parent column s an extend the LB listbox to the bottom of the screen. If I add width = 10, height = 75 to the LB definition line it does extend, but of course the bottom of LB will be lost when the window is resized. I realize this is really messy code but I think it explains the problem. Can someone tell me what to do to the code (and especially LB) to it make usable on various screen sizes? Thank you very much in advance.

class Application(tk.Frame): # /5104330/
#
    def __init__(self, parent, *args, **kwargs): # /17466561/
        tk.Frame.__init__(self, parent, *args, **kwargs) # /17466561/
        self.parent = parent # root - /17466561/
# Makes column 1 three times wider than column 2
# THIS DOES NOT HAVE ANY EFFECT. WHY NOT?:
        self.parent.columnconfigure(0, weight=3)
        self.parent.columnconfigure(1, weight=1)
# Setup frame
        frame1 = tk.Frame(self)# /61989498/
        frame1.grid(row=0, column=0, rowspan = 20, columnspan = 8, sticky='nsew')
# Create widgets
        scrollbar = tk.Scrollbar(frame1, orient=tk.VERTICAL) # /10870855/, /32715745/
        self.LB = tk.Listbox(frame1, yscrollcommand=scrollbar.set)#, width = 10, height = 75)
        scrollbar.config(command=self.LB.yview)
# Pack the widgets
        self.LB.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=1)
# insert data
        for r in range(0, 50):
           self.LB.insert(tk.END, str(r)+'-LB1')
# As per https://stackoverflow.com/questions/31885234/, /53073534/ and /64545856/
        for i in range(0,35):
# None of these extend the self.LB down to the bottom of the window:
            self.parent.grid_rowconfigure(i, weight=1)
            frame1.grid_rowconfigure(i, weight=1)
            self.LB.grid_rowconfigure(i, weight=1)
#
            self.parent.rowconfigure(i, weight=1)
            frame1.rowconfigure(i, weight=1)
            self.LB.rowconfigure(i, weight=1)
#
        frame2 = tk.Frame(self, bd=1, relief='flat', bg='white')# , width = 12, height = 700) # /61989498/
        frame2.grid(row=1, column=9, sticky='nsew', rowspan=40, columnspan = 3, ipadx=4)
        self.slbl = tk.Label(frame2, text ='Enter Search Term:'); self.slbl.pack(side=tk.TOP, padx=20)
if __name__ == "__main__":  # /17466561/
    root = tk.Tk()
    Application(root).pack(side='top', fill='both', expand=True)
    root.attributes('-zoomed', True) # maximize the window
    root.mainloop()

Upvotes: 0

Views: 87

Answers (1)

John
John

Reputation: 222

While I dislike answering my own question, I think the solution is just to remove the pack commands and use grid on the root window to get around the problem. Simplifying I have used:

import tkinter as tk
class Application(tk.Frame):
#
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
# Create widgets
        scrollbar = tk.Scrollbar(self.parent, orient=tk.VERTICAL)
        self.LB = tk.Listbox(self.parent, yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.LB.yview)
# Pack the widgets
        self.LB.grid(row=0, column=0, rowspan = 98, columnspan = 8, sticky='nsew')
        scrollbar.grid(row=0, column=8, rowspan = 98, columnspan = 1, sticky='nsw')
# insert data
        for r in range(0, 50):
           self.LB.insert(tk.END, str(r)+'-LB1')
        for i in range(0,98):
# Extend the self.LB down to the bottom of the maximized window
            self.parent.grid_rowconfigure(i, weight=1)
        self.slbl = tk.Label(self.parent, text ='Enter Search Term:')
        self.slbl.grid(row=1, column=9, sticky='nsew', rowspan=40, columnspan = 3, ipadx=4)
if __name__ == "__main__":
    root = tk.Tk()
    Application(root)
    root.attributes('-zoomed', True) # maximize the window
    root.mainloop()
##########################################################

Upvotes: 1

Related Questions