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

Thursday, 14 July 2016

Python Code Combo box example

Python Combobox example:

on Enter event, when the enter key is pressed. changes text to uppercase. adds item to list if not in the drop down list. highlights, selects all text, this allows you to keep typing a new symbol without placing your mouse in the box and erasing the previous symbol so you can type a new one.

# Craig Hammond 2016
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()  
   def create_widgets(self):  
     """ create the window layout. """  
     myFont = ('Lucida Grande', 12)  
     #create Label Symbol  
     self.label2 = Label(self, font=myFont, text="Symbol").grid(row=0, column=0)  
     # create combobox for the symbol  
     self.cbSymbol = ttk.Combobox(self, font=myFont, width=7, textvariable = varSymbol)  
     self.cbSymbol.bind("<Return>", self.cbSymbol_onEnter) #when the enter key is press an event happens  
     self.cbSymbol.bind('<<ComboboxSelected>>',self.cbSymbol_onEnter)  
     self.cbSymbol['values'] = ('AAPL','FB','NFLX')  
     self.cbSymbol.grid(row=0, column =1,sticky = W)  
   def cbSymbol_onEnter(self, event):  
     # changes characters to upper case  
     varSymbol.set(varSymbol.get().upper())  
     # gets the value of the text in the combobox. cbSymbol  
     mytext = varSymbol.get()  
     # gets list of values from dropdwn list of  
     # cbSymbol combobox  
     vals = self.cbSymbol.cget('values')  
     # selects all in the combobox. cbSymbol  
     self.cbSymbol.select_range(0, END)  
     print(mytext)  
     # checks if symbol exists in the combobox if not it adds it  
     # to the dropdown list  
     if not vals:  
       self.cbSymbol.configure(values = (mytext, ))  
     elif mytext not in vals:   
       self.cbSymbol.configure(values = vals + (mytext, ))  
     return 'break'   
root = Tk()  
root.title("Combobox")  
root.geometry("600x480")  
varSymbol = StringVar(root, value='NFLX')  
app = Application(root)  
root.mainloop()  
Spinbox Example Link

Learn how to increase the increment value programmatically

use the Control key and mouse click to increase the value by 10 cents and back to 1 cent   Save