1 / 18

More Widgets Entry , Checkbuttons, Radio Buttons, Scale

More Widgets Entry , Checkbuttons, Radio Buttons, Scale. CS360 Dick Steflik. Message. The Message widget is used to display text, this is a widget not a text window. Use for on screen instructions or to make a custom message window. from Tkinter import *

jerrod
Télécharger la présentation

More Widgets Entry , Checkbuttons, Radio Buttons, Scale

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More WidgetsEntry , Checkbuttons, Radio Buttons, Scale CS360 Dick Steflik

  2. Message The Message widget is used to display text, this is a widget not a text window. Use for on screen instructions or to make a custom message window from Tkinter import * msg = Message(‘Now is the time for all good men to come to the aid of their country’) msg.config(font=‘times’,14)) msg.pack() mainloop() options: anchor padx aspect pady background/bg relief borderwidth/bd takefocus cursor text font textvariable foreground/fg width highlightbackground highlightcolor highlightthickness justify

  3. Entry options: anchor aspect background/bg relief borderwidth/bd takefocus cursor text font textvariable foreground/fg width highlightbackground validate highlightcolor validatecommand highlightthickness justify methods: get() set() delete(first,last/END) insert(index) insert(index,string) icursor(index) index(index)

  4. Entry validate - when to validate; “none”, “focus”, “focusin”, “focusout”, “key” , “all” validatecommand – the name of the function to use for validation Tabbing – Tab order is the same as the order in which the widgets were created

  5. Checkbuttons and Radiobuttons • Checkbuttons are used for multiple choice situations, i.e. choosing m of n possible options. This is done by assigning each checkbox a variable of its own. • Radiobuttions are used for choosing one of n possible choices; i.e. a mutually exclusive single choice by giving each button a unique value of the same Tkinter variable

  6. Checkbutton from Tkinter import * def cb(): print "variable is", var.get() win = Tk() var = IntVar() c = Checkbutton( win, text="Enable Tab", variable=var, command= (lambda: cb())) c.pack() mainloop()

  7. from Tkinter import * def cb(): print "beer is", var1.get() print "Wine is", var2.get() print "Water is", var3.get() win = Tk() f = Frame(relief=RAISED , borderwidth=5) var1 = IntVar() var2 = IntVar() var3 = IntVar() c1 = Checkbutton( f, text="Beer", variable=var1, command= (lambda: cb())) c1.pack(side=TOP) c2 = Checkbutton( f, text="Wine", variable=var2, command= (lambda: cb())) c2.pack(side=TOP) c3 = Checkbutton( f, text="Water", variable=var3, command= (lambda: cb())) c3.pack(side=TOP) f.pack() mainloop()

  8. Radiobuttons from Tkinter import * def change(): print 'Station = ' , var.get() root = Tk() stations = 'WAAL' , 'WSKG' , 'WSQX' , 'WNBF' f = Frame(relief=RAISED , borderwidth=5) var = StringVar() for station in stations: radio = Radiobutton(f, text=station,variable=var ,value=station) radio.pack(side=TOP) f.pack(pady=10) Button(root,text='New' , command=(lambda: change())).pack(pady=10) var.set('WAAL') #initalize the set of radio buttons mainloop()

  9. Scale/Sliders from Tkinter import * class SliderDemo(Frame): def __init__(self,parent=None): Frame.__init__(self,parent) self.pack() self.var = IntVar() Scale(self,label='Miles', command=self.onMove, variable = self.var, from_=0 , to=100 ,length=200, tickinterval=20).pack() Button(self , text='Read', command=self.readScale).pack(pady=10) def onMove(self, value): print 'onMove = ' , value def readScale(self): print 'readscale = ' , self.var.get() if __name__ == '__main__' : SliderDemo().mainloop()

  10. Listboxes and Scrollbars from Tkinter import * class ScrolledList(Frame): def __init__(self,options,parent=None): Frame.__init__(self,parent) self.pack(expand=YES , fill=BOTH) self.makeWidgets(options) def handleList(self,event): index = self.list.curselection() label = self.list.get(index) self.runCommand(label) def makeWidgets(self , options): sbar = Scrollbar(self) list = Listbox(self,relief=SUNKEN) sbar.config(command=list.yview) list.config(yscrollcommand=sbar.set) sbar.pack(side=RIGHT , fill=Y) list.pack(side=LEFT , expand=YES , fill=BOTH) pos = 0 for label in options: list.insert(pos,label) pos += 1 #list.config(selectmode=SINGLE , setgrid = 1) list.bind('<Double-1>' , self.handleList) self.list = list def runCommand(self,selection): print 'You selected ' , selection if __name__ == '__main__' : options = map((lambda x : 'My Choice - ' + str(x)) , range(20)) ScrolledList(options).mainloop() Dick@BIGBOY /cygdrive/c/cs260/TkinterExamples $ python listbox.py You selected My Choice - 12 You selected My Choice - 15 You selected My Choice - 8 You selected My Choice - 10

  11. Canvas The canvas widget is a free form device it can be used to draw graphs and plots, create graphics editors, and implement various kinds of custom widgets. Provides basic drawing facilities, as well as advanced drawing features Drawing is done by creating canvas items Items are not widgets, even though they are handled in a similar way Each item receives a unique ID upon creation Each item is enclosed in its bounding box specified by a top-left corner and a lower-right corner

  12. Items Supported by Canvas • Arc : arc, chord, pieslice • Bitmap • Image • Line • Oval: circle or ellipse • Polygon • Rectangle • Texy • Window: used to place other widgets on the canvas (ex buttons)

  13. Canvas Options • Option: • closeenough • confine • scrollregion • xscrollcommand, yscrollcommand • xscrollincrement, yscrollincrement • Possible Values: • Float • Boolean • list of 4 coordinates • Function • Distance

  14. Manipulating Items • Item creation functions (create_line( ),create_oval( ), etc.) all return the item ID of the newly created canvas item • The itemconfigure( ) method is used to configure canvas items after their creation(See Tkinter documentation Online

  15. Coordinate System • 2 coordinate systems: • Canvas coordinate system: origin at the topleft corner of the canvas (may not be visible) • Window coordinate system: origin at the topleft corner of the visible portion of the canvas • Event objects use the Window coord. system • canvasx( ) and canvasy( ) methods can convert Window coordinates to Canvas coordinates

  16. Canvas and Tags • Tags are strings that can be associated with any canvas item • More than one item can have the same tag, and a single item can have multiple tags • This allows you to create groups of items • Canvas items can be interchangeably referenced by ID (integers) or tags.

  17. Canvas and Tags (more) • What happens to function which only take one item as parameter? • Tkinter provides a very good approach:the first (lowest) item in the display list that matches the tag is used • Binding can also be done on canvas items by using the tag_bind( ) and tag_unbind( ) functions

  18. Special Tags • CURRENT (“current”): the item that is currently situated under the mouse cursor is automatically assigned the CURRENT tag. (Note: don’t use this tag manually!!) • ALL (“all”): this special tag matches all items in the canvas

More Related