Dec-28-2021, 03:36 PM
One way
#! /usr/bin/env python3 import tkinter as tk import sys class Window: def __init__(self, parent): self.counter = 0 container = tk.Frame(parent) container.grid(column=0, row=0, sticky='new') self.label = tk.Label(container) self.label['text'] = 'Counter: 0' self.label.grid(column=0, row=0, sticky='new') btnframe = tk.Frame(parent) btnframe.grid(column=0, row=1, sticky='new') for i in range(3): btnframe.grid_columnconfigure(i, weight=3, uniform='btns') btn1 = tk.Button(btnframe, text='Up', command=self.up) btn1.grid(column=0, row=1, sticky='new') btn2 = tk.Button(btnframe, text='Down', command=self.down) btn2.grid(column=1, row=1, sticky='new') btn3 = tk.Button(btnframe, text='Exit', command=sys.exit) btn3.grid(column=2, row=1, sticky='new') def up(self): self.counter += 1 self.label['text'] = f'Counter: {self.counter}' def down(self): if self.counter < 1: self.counter = 0 else: self.counter -= 1 self.label['text'] = f'Counter: {self.counter}' def main(): root = tk.Tk() root['padx'] = 5 root['pady'] = 3 Window(root) root.mainloop() main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags