i'm making first gui application , i've run silly problem. resizing main window doesn't resize contents , leaves blank space. i've read tkdocs , should use sticky , column/row weight attributes don't understand how work. here's code (only part covering widgets, if think problem isn't here i'll post rest of it):
from tkinter import * tkinter import ttk root = tk() mainframe = ttk.frame(root, padding="3 3 12 12") mainframe.grid(column=0, row=0, sticky=(n, w, e, s)) mainframe.columnconfigure(0, weight=1) mainframe.rowconfigure(0, weight=1) player1 = stringvar() player2 = stringvar() player1.set('player 1') player2.set('player 1') timer=stringvar() running=booleanvar() running.set(0) settimer = ttk.entry(mainframe, width=7, textvariable=timer) settimer.grid(column=2, row=1, sticky=(n, s)) ttk.button(mainframe, text="start", command=start).grid(column=2, row=2, sticky=(n, s)) ttk.label(mainframe, textvariable=player1, font=timefont).grid(column=1, row=3, sticky=(w, s)) ttk.label(mainframe, textvariable=player2, font=timefont).grid(column=3, row=3, sticky=(e, s)) child in mainframe.winfo_children(): child.grid_configure(padx=80, pady=10) root.mainloop()
thanks time!
maybe in right direction. sure configure column/row weights @ each level.
import tkinter.ttk tkinter.constants import * class application(tkinter.ttk.frame): @classmethod def main(cls): tkinter.nodefaultroot() root = tkinter.tk() app = cls(root) app.grid(sticky=nsew) root.grid_columnconfigure(0, weight=1) root.grid_rowconfigure(0, weight=1) root.resizable(true, false) root.mainloop() def __init__(self, root): super().__init__(root) self.create_variables() self.create_widgets() self.grid_widgets() self.grid_columnconfigure(0, weight=1) def create_variables(self): self.player1 = tkinter.stringvar(self, 'player 1') self.player2 = tkinter.stringvar(self, 'player 2') self.timer = tkinter.stringvar(self) self.running = tkinter.booleanvar(self) def create_widgets(self): self.set_timer = tkinter.ttk.entry(self, textvariable=self.timer) self.start = tkinter.ttk.button(self, text='start', command=self.start) self.display1 = tkinter.ttk.label(self, textvariable=self.player1) self.display2 = tkinter.ttk.label(self, textvariable=self.player2) def grid_widgets(self): options = dict(sticky=nsew, padx=3, pady=4) self.set_timer.grid(column=0, row=0, **options) self.start.grid(column=0, row=1, **options) self.display1.grid(column=0, row=2, **options) self.display2.grid(column=0, row=3, **options) def start(self): timer = self.timer.get() self.player1.set(timer) self.player2.set(timer) if __name__ == '__main__': application.main()
Comments
Post a Comment