Advertisement
steve-shambles-2109

Python tkinter Spinbox self contained example v1.3

Nov 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. """Spinbox, self contained example v1.3
  2.   By Steve Shambles. Upadted Nov 2019
  3.  
  4.   Plenty more outrageous code atrocities at:
  5.   stevepython.wordpress.com
  6.   """
  7.  
  8. from tkinter import Button, IntVar, LabelFrame
  9. from tkinter import messagebox, N, RIDGE, Spinbox, TclError, Tk, W
  10.  
  11. root = Tk()
  12. root.title('Spinbox V1.2')
  13. root.geometry('180x90')
  14. root.resizable(False, False)
  15.  
  16. def spn_bx_val():
  17.     """Get value from the spinbox. Show value in a pop up."""
  18.  
  19.     # If input is invalid, eg.non-numeric, then return
  20.     try:
  21.         spv = VAR.get()
  22.     except TclError:
  23.         return
  24.  
  25.     messagebox.showinfo('Spinbox', 'Spinbox value is currently '+str(spv))
  26.  
  27. # Create labelframe for the spinbox.
  28. spinbox_frame = LabelFrame(root, relief=RIDGE,
  29.                            fg='blue', text='Spinbox')
  30. spinbox_frame.grid(sticky=N, padx=10, pady=13)
  31.  
  32. # Create the spinbox.
  33. VAR = IntVar()
  34. VAR.set(50) # Default start value.
  35. SPIN = Spinbox(spinbox_frame, from_=0, to=100, width=4, textvariable=VAR)
  36.  
  37. # Create the 'spinbox value' button.
  38. Button(spinbox_frame, bg='green2', text='Spinbox value',
  39.        command=spn_bx_val).grid(row=0, column=1, sticky=W)
  40. SPIN.grid(column=0, row=0, padx=10, pady=10)
  41.  
  42. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement