Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2025
20
0
363 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | Source Code | 0 0
  1. from tkinter import *
  2. from tkinter import ttk, messagebox
  3.  
  4. class Application:
  5.     def __init__(self, root):
  6.         root.title('counter example')
  7.        
  8.         mainFrame = ttk.Frame(root, padding = '4 4 4 4')
  9.         mainFrame.grid(column=0, row=0, sticky=(N, W, S, E))
  10.         root.columnconfigure(0, weight=1)
  11.         root.rowconfigure(0, weight=1)
  12.        
  13.         incrButton = ttk.Button(mainFrame, text = 'Up', command = self.incrButtonClick)
  14.         incrButton.grid(column=0, row=0, padx=2, pady=2, sticky=(N, S))
  15.        
  16.         decrButton = ttk.Button(mainFrame, text = 'Dn', command = self.decrButtonClick)
  17.         decrButton.grid(column=0, row=1, padx=2, pady=2, sticky=(N, S))
  18.        
  19.         resetButton = ttk.Button(mainFrame, text = 'Reset', command = self.resetButtonClick)
  20.         resetButton.grid(column=0, row=2, padx=2, pady=2, sticky=(N, S))
  21.        
  22.         self.countValue = StringVar()
  23.         countEntry = ttk.Entry(mainFrame, width=3, justify = CENTER, font=('Aptos', 28, 'bold'), textvariable=self.countValue)
  24.         countEntry.grid(column=1, row=0, padx=2, pady=2, rowspan=3, sticky=(N, S, E, W))
  25.         self.countValue.set('42')
  26.        
  27.     def incrButtonClick(self, **args):
  28.         val = int(self.countValue.get())
  29.         if val == 52:
  30.             messagebox.showinfo(title = 'Stop!!!', message='Meh - the number can\'t go any higher!', icon='error')
  31.         else:
  32.             self.countValue.set(str(val + 1))
  33.        
  34.     def decrButtonClick(self, **args):
  35.         val = int(self.countValue.get())
  36.         if val == 32:
  37.             messagebox.showinfo(title = 'Stop!!!', message='Meh - the number can\'t go any lower!', icon='error')
  38.         else:
  39.             self.countValue.set(str(val - 1))
  40.        
  41.     def resetButtonClick(self, **args):
  42.         val = int(self.countValue.get())
  43.         if val == 42:
  44.             messagebox.showinfo(title = 'Stop!!!', message="It's already 42 you numpty!", icon='error')
  45.         else:
  46.             self.countValue.set('42')
  47.        
  48. def main():
  49.     root = Tk()
  50.     Application(root)
  51.     root.mainloop()
  52.    
  53. if __name__ == '__main__':
  54.     main()
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement