Advertisement
gregwa

Tkinter Widget Demo Article #26

Aug 3rd, 2011
609
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # widgetdemo1f.py
  2. # Full application
  3.  
  4. from Tkinter import *
  5. import tkMessageBox
  6.  
  7. class Demo:
  8.     def __init__(self,master):
  9.         self.DefineVars()
  10.         f = self.BuildWidgets(master)
  11.         self.PlaceWidgets(f)
  12.        
  13.     def BuildWidgets(self,master):
  14.         # Define our widgets
  15.         frame = Frame(master)
  16.         # Labels
  17.         self.lblframe = Frame(frame,relief = SUNKEN,padx = 3, pady = 3, borderwidth = 2, width = 500)
  18.         self.lbl1 = Label(self.lblframe,text="Flat Label",relief = FLAT, width = 13,borderwidth = 2)
  19.         self.lbl2 = Label(self.lblframe,text="Sunken Label", relief = SUNKEN, width = 13, borderwidth = 2)
  20.         self.lbl3 = Label(self.lblframe,text="Ridge Label", relief = RIDGE, width = 13, borderwidth = 2)
  21.         self.lbl4 = Label(self.lblframe,text="Raised Label", relief = RAISED, width = 13, borderwidth = 2)
  22.         self.lbl5 = Label(self.lblframe,text="Groove Label", relief = GROOVE,width = 13, borderwidth = 2)
  23.         # Buttons
  24.         self.btnframe = Frame(frame,relief = SUNKEN,padx = 3, pady = 3, borderwidth = 2, width = 500)
  25.         self.btn1 = Button(self.btnframe,text="Flat Button", relief = FLAT, borderwidth = 2)
  26.         self.btn2 = Button(self.btnframe,text="Sunken Button", relief = SUNKEN, borderwidth = 2)
  27.         self.btn3 = Button(self.btnframe,text="Ridge Button", relief = RIDGE, borderwidth = 2)
  28.         self.btn4 = Button(self.btnframe,text="Raised Button", relief = RAISED, borderwidth = 2)
  29.         self.btn5 = Button(self.btnframe,text="Groove Button", relief = GROOVE, borderwidth = 2)
  30.         self.btn1.bind('<ButtonRelease-1>',lambda e: self.BtnCallback(1))
  31.         self.btn2.bind('<ButtonRelease-1>',lambda e: self.BtnCallback(2))
  32.         self.btn3.bind('<ButtonRelease-1>',lambda e: self.BtnCallback(3))
  33.         self.btn4.bind('<ButtonRelease-1>',lambda e: self.BtnCallback(4))
  34.         self.btn5.bind('<ButtonRelease-1>',lambda e: self.BtnCallback(5))
  35.         # Check Boxes
  36.         self.cbframe = Frame(frame, relief = SUNKEN, padx = 3, pady = 3, borderwidth = 2, width = 500)
  37.         self.chk1 = Checkbutton(self.cbframe, text = "Normal Checkbox",variable=self.Chk1Val)
  38.         self.chk2 = Checkbutton(self.cbframe, text = "Checkbox",variable=self.Chk2Val,indicatoron = False)
  39.         self.chk1.bind('<ButtonRelease-1>',lambda e: self.ChkBoxClick(1))
  40.         self.chk2.bind('<ButtonRelease-1>',lambda e: self.ChkBoxClick(2))
  41.         self.btnToggleCB = Button(self.cbframe,text="Toggle Cbs")
  42.         self.btnToggleCB.bind('<ButtonRelease-1>',self.btnToggle)
  43.         # Radio Buttons
  44.         self.rbframe = Frame(frame, relief = SUNKEN, padx = 3, pady = 3, borderwidth = 2, width = 500)
  45.         self.rb1 = Radiobutton(self.rbframe, text = "Radio 1", variable = self.RBVal, value = 1)
  46.         self.rb2 = Radiobutton(self.rbframe, text = "Radio 2", variable = self.RBVal, value = 2)
  47.         self.rb3 = Radiobutton(self.rbframe, text = "Radio 3", variable = self.RBVal, value = 3)
  48.         self.rb1.bind('<ButtonRelease-1>',lambda e: self.RBClick())
  49.         self.rb2.bind('<ButtonRelease-1>',lambda e: self.RBClick())
  50.         self.rb3.bind('<ButtonRelease-1>',lambda e: self.RBClick())
  51.         self.rb4 = Radiobutton(self.rbframe, text = "Radio 4", variable = self.RBVal2, value = "1-1")
  52.         self.rb5 = Radiobutton(self.rbframe, text = "Radio 5", variable = self.RBVal2, value = "1-2")
  53.         self.rb6 = Radiobutton(self.rbframe, text = "Radio 6", variable = self.RBVal2, value = "1-3")
  54.         self.rb4.bind('<ButtonRelease-1>',lambda e: self.RBClick2())
  55.         self.rb5.bind('<ButtonRelease-1>',lambda e: self.RBClick2())
  56.         self.rb6.bind('<ButtonRelease-1>',lambda e: self.RBClick2())                            
  57.         # Text Boxes
  58.         self.tbframe = Frame(frame, relief = SUNKEN, padx = 3, pady = 3, borderwidth = 2, width = 500)
  59.         self.txt1 = Entry(self.tbframe, width = 10)
  60.         self.txt2 = Entry(self.tbframe, disabledbackground="#cccccc", width = 10)
  61.         self.btnDisable = Button(self.tbframe, text = "Enable/Disable")
  62.         self.btnDisable.bind('<ButtonRelease-1>', self.btnDisableClick)
  63.         self.lstframe = Frame(frame, relief = SUNKEN, padx = 3, pady = 3, borderwidth = 2, width = 500)
  64.         # Scrollbar for list box
  65.         self.VScroll = Scrollbar(self.lstframe)
  66.         # List Box
  67.         self.lbox = Listbox(self.lstframe, height = 5,yscrollcommand = self.VScroll.set) # default height is 10
  68.         self.lbox.bind('<<ListboxSelect>>',self.LBoxSelect)
  69.         self.VScroll.config(command = self.lbox.yview)
  70.         self.btnClearLBox = Button(self.lstframe,text = "Clear List",command = self.ClearList,width = 11)
  71.         self.btnFillLBox = Button(self.lstframe,text = "Fill List",command = self.FillList, width = 11)          
  72.         # <<ListboxSelect>> is virtual event
  73.         # Fill the list box
  74.         self.FillList()
  75.      
  76.         # Buttons to show message boxes and dialogs
  77.         self.mbframe = Frame(frame,relief = SUNKEN,padx = 3, pady = 3, borderwidth = 2)
  78.         self.btnMBInfo = Button(self.mbframe,text = "Info")
  79.         self.btnMBWarning = Button(self.mbframe,text = "Warning")
  80.         self.btnMBError = Button(self.mbframe,text = "Error")
  81.         self.btnMBQuestion = Button(self.mbframe,text = "Question")
  82.         self.btnMBYesNo = Button(self.mbframe,text = "Yes/No")
  83.         self.btnMBInfo.bind('<ButtonRelease-1>', lambda e: self.ShowMessageBox(1))
  84.         self.btnMBWarning.bind('<ButtonRelease-1>', lambda e: self.ShowMessageBox(2))
  85.         self.btnMBError.bind('<ButtonRelease-1>', lambda e: self.ShowMessageBox(3))
  86.         self.btnMBQuestion.bind('<ButtonRelease-1>', lambda e: self.ShowMessageBox(4))
  87.         self.btnMBYesNo.bind('<ButtonRelease-1>', lambda e: self.ShowMessageBox(5))
  88.         return frame
  89.        
  90.     def PlaceWidgets(self, master):
  91.         frame = master
  92.         # Place the widgets
  93.         frame.grid(column = 0, row = 0)
  94.         # Place the labels
  95.         self.lblframe.grid(column = 0, row = 1, padx = 5, pady = 5, columnspan = 5,sticky='WE')
  96.         l = Label(self.lblframe,text='Labels |',width=15, anchor='e').grid(column=0,row=0)
  97.         self.lbl1.grid(column = 1, row = 0, padx = 3, pady = 5)
  98.         self.lbl2.grid(column = 2, row = 0, padx = 3, pady = 5)
  99.         self.lbl3.grid(column = 3, row = 0, padx = 3, pady = 5)
  100.         self.lbl4.grid(column = 4, row = 0, padx = 3, pady = 5)
  101.         self.lbl5.grid(column = 5, row = 0, padx = 3, pady = 5)
  102.         # Place the buttons
  103.         self.btnframe.grid(column=0, row = 2, padx = 5, pady = 5, columnspan = 5,sticky = 'WE')
  104.         l = Label(self.btnframe,text='Buttons |',width=15, anchor='e').grid(column=0,row=0)
  105.         self.btn1.grid(column = 1, row = 0, padx = 3, pady = 3)
  106.         self.btn2.grid(column = 2, row = 0, padx = 3, pady = 3)
  107.         self.btn3.grid(column = 3, row = 0, padx = 3, pady = 3)
  108.         self.btn4.grid(column = 4, row = 0, padx = 3, pady = 3)
  109.         self.btn5.grid(column = 5, row = 0, padx = 3, pady = 3)    
  110.         # Place the Checkboxes and toggle button
  111.         self.cbframe.grid(column = 0, row = 3, padx = 5, pady = 5, columnspan = 5,sticky = 'WE')
  112.         l = Label(self.cbframe,text='Check Boxes |',width=15, anchor='e').grid(column=0,row=0)
  113.         self.btnToggleCB.grid(column = 1, row = 0, padx = 3, pady = 3)                  
  114.         self.chk1.grid(column = 2, row = 0, padx = 3, pady = 3)
  115.         self.chk2.grid(column = 3, row = 0, padx = 3, pady = 3)
  116.         # Place the Radio Buttons and select the first one
  117.         self.rbframe.grid(column = 0, row = 4, padx = 5, pady = 5, columnspan = 5,sticky = 'WE')
  118.         l = Label(self.rbframe,text='Radio Buttons |',width=15,anchor='e').grid(column=0,row=0)
  119.         self.rb1.grid(column = 2, row = 0, padx = 3, pady = 3, sticky = 'EW')
  120.         self.rb2.grid(column = 3, row = 0, padx = 3, pady = 3, sticky = 'WE')
  121.         self.rb3.grid(column = 4, row = 0, padx = 3, pady = 3, sticky = 'WE')
  122.         self.RBVal.set("1")        
  123.         l = Label(self.rbframe,text='| Another Set |',width = 15, anchor = 'e').grid(column = 5, row = 0)
  124.         self.rb4.grid(column = 6, row = 0)
  125.         self.rb5.grid(column = 7, row = 0)
  126.         self.rb6.grid(column = 8, row = 0)
  127.         self.RBVal2.set("1-1")            
  128.         # Place the Text boxes
  129.         self.tbframe.grid(column = 0, row = 5, padx = 5, pady = 5, columnspan = 5,sticky = 'WE')
  130.         l = Label(self.tbframe,text='Text Boxes |',width=15, anchor='e').grid(column=0,row=0)
  131.         self.txt1.grid(column = 2, row = 0, padx = 3, pady = 3)
  132.         self.txt2.grid(column = 3, row = 0, padx = 3, pady = 3)
  133.         self.btnDisable.grid(column = 1, row = 0, padx = 3, pady = 3)
  134.         # Place the Listbox and support buttons
  135.         self.lstframe.grid(column = 0, row = 6, padx = 5, pady = 5, columnspan = 5,sticky = 'WE')
  136.         l = Label(self.lstframe,text='List Box |',width=15, anchor='e').grid(column=0,row=0,rowspan=2)
  137.         self.lbox.grid(column = 2, row = 0,rowspan=2)
  138.         self.VScroll.grid(column = 3, row = 0,rowspan = 2, sticky = 'NSW')
  139.         self.btnClearLBox.grid(column = 1, row = 0, padx = 5)
  140.         self.btnFillLBox.grid(column = 1, row = 1, padx = 5)        
  141.         # Messagebox buttons and frame
  142.         self.mbframe.grid(column = 0,row = 7, columnspan = 5, padx = 5, sticky = 'WE')
  143.         l = Label(self.mbframe,text='Message Boxes |',width=15, anchor='e').grid(column=0,row=0)
  144.         self.btnMBInfo.grid(column = 1, row = 0, padx= 3)
  145.         self.btnMBWarning.grid(column = 2, row = 0, padx= 3)
  146.         self.btnMBError.grid(column = 3, row = 0, padx= 3)
  147.         self.btnMBQuestion.grid(column = 4, row = 0, padx= 3)
  148.         self.btnMBYesNo.grid(column = 5, row = 0, padx= 3)
  149.                
  150.     def DefineVars(self):
  151.         # Define our resources
  152.         self.Chk1Val = IntVar()
  153.         self.Chk2Val = IntVar()
  154.         self.RBVal = IntVar()
  155.         self.RBVal2 = StringVar()                
  156.         self.Disabled = False
  157.         # List for List box items
  158.         self.examples = ['Item One','Item Two','Item Three','Item Four']        
  159.        
  160.     def BtnCallback(self,val):
  161.         if val == 1:
  162.             print("Flat Button Clicked...")
  163.         elif val == 2:
  164.             print("Sunken Button Clicked...")
  165.         elif val == 3:
  166.             print("Ridge Button Clicked...")
  167.         elif val == 4:
  168.             print("Raised Button Clicked...")
  169.         elif val == 5:
  170.             print("Groove Button Clicked...")
  171.                    
  172.     def btnToggle(self,p1):
  173.         self.chk1.toggle()
  174.         self.chk2.toggle()
  175.         print("Check box 1 value is {0}".format(self.Chk1Val.get()))
  176.         print("Check box 2 value is {0}".format(self.Chk2Val.get()))        
  177.        
  178.     def ChkBoxClick(self,val):
  179.         if val == 1:
  180.             print("Check box 1 value is {0}".format(self.Chk1Val.get()))
  181.         elif val == 2:
  182.             print("Check box 2 value is {0}".format(self.Chk2Val.get()))
  183.  
  184.     def RBClick(self):
  185.         print("Radio Button clicked - Value is {0}".format(self.RBVal.get()))        
  186.     def RBClick2(self):
  187.         print("Radio Button clicked - Value is {0}".format(self.RBVal2.get()))            
  188.     def btnDisableClick(self,p1):
  189.         if self.Disabled == False:
  190.             self.Disabled = True
  191.             self.txt2.configure(state='disabled')
  192.         else:
  193.             self.Disabled = False
  194.             self.txt2.configure(state='normal')
  195.            
  196.     def ClearList(self):
  197.         self.lbox.delete(0,END)
  198.        
  199.     def FillList(self):
  200.         # Note, clear the listbox first...no check is done
  201.         for ex in self.examples:
  202.             self.lbox.insert(END,ex)  
  203.         # insert([0,ACTIVE,END],item)
  204.  
  205.     def LBoxSelect(self,p1):
  206.         print("Listbox Item clicked")
  207.         items = self.lbox.curselection()
  208.         selitem = items[0]
  209.         print("Index of selected item = {0}".format(selitem))
  210.         print("Text of selected item = {0}".format(self.lbox.get(selitem)))
  211.        
  212.     def ShowMessageBox(self,which):
  213.         if which == 1:
  214.             tkMessageBox.showinfo('Demo','This is an INFO messagebox')
  215.         elif which == 2:
  216.             tkMessageBox.showwarning('Demo','This is a WARNING messagebox')
  217.         elif which == 3:
  218.             tkMessageBox.showerror('Demo','This is an ERROR messagebox')
  219.         elif which == 4:
  220.             resp = tkMessageBox.askquestion('Demo','This is a QUESTION messagebox?')
  221.             print('{0} was pressed...'.format(resp))
  222.         elif which == 5:
  223.             resp = tkMessageBox.askyesno('Demo','This is a YES/NO messagebox')
  224.             print('{0} was pressed...'.format(resp))
  225.            
  226. root = Tk()
  227. root.geometry('750x490+550+150')
  228. demo = Demo(root)
  229.  
  230. root.mainloop()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement