Advertisement
Guest User

Python Tkinter Spinbox, self contained example v1.2

a guest
Nov 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. """Spinbox, self contained example v1.2
  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, 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 FloatingPointError:
  23.         return
  24.     # Note: I should state what exception error, like
  25.     # except FloatingPointError:
  26.     # but I can't get it to work, this works, though not pythonic.
  27.    
  28.     messagebox.showinfo('Spinbox', 'Spinbox value is currently '+str(spv))
  29.  
  30. # Create labelframe for the spinbox.
  31. spinbox_frame = LabelFrame(root, relief=RIDGE, fg='blue', text='Spinbox')
  32. spinbox_frame.grid(sticky=N, padx=10, pady=13)
  33.  
  34. # Create the spinbox.
  35. VAR = IntVar()
  36. VAR.set(50) # Default start value.
  37. SPIN = Spinbox(spinbox_frame, from_=0, to=100, width=4, textvariable=VAR)
  38.  
  39. # Create the 'spinbox value' button.
  40. Button(spinbox_frame, bg='green2', text='Spinbox value',
  41.        command=spn_bx_val).grid(row=0, column=1, sticky=W)
  42. SPIN.grid(column=0, row=0, padx=10, pady=10)
  43.  
  44. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement