Sunday, 17 July 2016

Python Spinbox example

Change spinbox increment value programmatically from 10 cents to 1 cent, using control click arrow up or down. Format for 8 characters and 2 decimal places, this should be include otherwise you can get different numbers.

 Please comment below if you have a better way of doing the same basic thing. please see link below for other example using a combobox

 # Craig Hammond  
 from tkinter import *  
 from tkinter import ttk  
 class Application(Frame):  
   """A GUI application """ 
 
   def __init__(self, master):  
     """ Initialize the Frame"""  
     ttk.Frame.__init__(self, master)  
     self.grid()  
     self.create_widgets()
     self.increment_switch = True  # used as a switch

   def create_widgets(self):  
     """ create the window layout. """  
     myFont = ('Lucida Grande', 12)  
     #create Label Limit Price  
     self.label2 = Label(self, font=myFont, text="Limit Price").grid(row=0, column=0)   
     # create a Spinbox  
     self.spinLimitPrice = Spinbox(self, font=myFont, format='%8.2f',  
                    increment=.01, from_=0.0, to=1000.0, width=7,  
                    textvariable=varLimitPrice)  
     # when control and up or down arrow are pressed call spinLimitDime()  
     self.spinLimitPrice.bind('<Control-Button-1>', self.spinLimitDime)  
     self.spinLimitPrice.grid(row=2, column=3)

   def spinLimitIncrement(self, event):
       if self.increment_switch == True:
           self.spinLimitPrice.configure(increment=0.10)
           self.increment_switch = False
       else:
           self.spinLimitPrice.configure(increment=0.01)
           self.increment_switch = True 
 root = Tk()  
 root.title("Spinbox")  
 root.geometry("600x480")  
 varLimitPrice = StringVar()  
 app = Application(root)  
 root.mainloop()  
If you have a better way of doing this, please comment below Python code for combobox link
Save

No comments:

Post a Comment