ARTICLE AD BOX
I am trying to build a simple app in python with tkinter. It contains two rows for file selection (column of labels on frame1, labelFrame, and column of entry and button pairs on a second frame, addressFrame), as well as two buttons in the bottom right corner, which for the purpose of this post are just placeholders that I kept for the full layout.
The window looks like this: Main view. The problem comes when I try to resize it. I want it to still be able to be minimized like this:Minimized view, but when I stretch it out, there is a lot of blank space created in between labelFrame and addressFrame, which I don't want: Maximazed view
I want labelFrame to shorten together with addressFrame when I am minimizing the window, but when expanding I want to it to resize only to a certain width (longest label length + a few pixels), while addressFrame (basically just file address in the entry) continues to expand. It seems that I can only have one or the other: either labelFrame's width is completely fixed and does not minimize until addressFrame disappears from view when I move the mainframe, or stretching it creates extra space.
Please help.
Here is the code:
from tkinter import * # from tkinter import Tk for Python 3.x from tkinter import ttk from tkinter import filedialog def validateInputs(): #placeholder return True def calculate(): #placeholder return True class FileSelect(Frame): def __init__(self,parent=None,**kw): Frame.__init__(self,master=parent,**kw) self.filePath = StringVar() self.entPath = Entry(self, width=50, textvariable=self.filePath) self.entPath.grid(row=0,column=0,sticky='nsew') self.btnFind = ttk.Button(self, text="...",command=self.setFolderPath) self.btnFind.grid(row=0,column=1,sticky='e') def setFolderPath(self): file_selected = filedialog.askopenfilename() # show an "Open" dialog box and return the path to the selected file self.filePath.set(file_selected) @property def file_path(self): return self.filePath.get() class labelFrame(ttk.Frame): def __init__(self, container): super().__init__(container) # setup the grid layout manager self.columnconfigure(0, weight=1) self.__create_widgets() def __create_widgets(self): ttk.Label(self, text='Choose file 1 with the main data:').grid(column=0, row=0, sticky='ew') ttk.Label(self, text='Choose file 2 for verification:').grid(column=0, row=1, sticky='ew') for widget in self.winfo_children(): widget.grid(padx=0, pady=5) class addressFrame(ttk.Frame): def __init__(self, container): super().__init__(container) # setup the grid layout manager self.columnconfigure(0, weight=3) self.__create_widgets() def __create_widgets(self): file1 = FileSelect(self) file1.grid(row=0, sticky='nsew') file1.columnconfigure(0, weight=3) file2 = FileSelect(self) file2.grid(row=1, sticky='nsew') file2.columnconfigure(0, weight=3) for widget in self.winfo_children(): widget.grid(padx=0, pady=3) #main module gui = Tk() gui.title("TEST") gui.columnconfigure(0, weight=1) gui.rowconfigure(0, weight=1) gui.minsize(400, 100) mainframe = ttk.Frame(gui, height=600, padding=(3, 3, 12, 12)) mainframe.grid(column=0, row=0, sticky='nsew') mainframe.columnconfigure(0, weight=1) mainframe.columnconfigure(1, weight=1) # create the label frame label_frame = labelFrame(mainframe) label_frame.grid(column=0, row=0, sticky='nswe') label_frame.columnconfigure(0, weight=1) # create the frame w file address entry and button address_frame = addressFrame(mainframe) address_frame.grid(column=1, row=0, sticky='nswe') address_frame.columnconfigure(0, weight=3) valBtn = ttk.Button(mainframe, text="Validate inputs", command = validateInputs) valBtn.grid(column=3, row=2, padx=5, pady=5, sticky='e') valBtn.state(['disabled']) btn = ttk.Button(mainframe, text="(Re)calculate", command = calculate) btn.grid(column=3, row=3, padx=5, pady=5, sticky='e') btn.state(['disabled']) for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) gui.mainloop()