Tkinter中的OptionMenu和ComboBox

本来是要找Tkinter的下拉框控件,标准库中竟然没有,于是上sf.net,发现Pmw这个好玩的东东。下面的代码来自Pmw的demo,我将其进行了简化。有关Pmw的更多详情可以见这里 http://tkinter.unpy.net/wiki/Pmw
OptionMenu
Tkinter中的OptionMenu和ComboBox
#coding:utf-8
import sys
import Tkinter
import Pmw

class Demo:
    def __init__(self, root):
	self.var = Tkinter.StringVar()
	self.var.set('steamed')
	self.method_menu = Pmw.OptionMenu(
	    root,
		labelpos = 'w',
		label_text = 'Choose method:',
		menubutton_textvariable = self.var,
		items = ['baked', 'steamed', 'stir fried', 'boiled', 'raw'],
		menubutton_width = 10,
	)
	self.method_menu.pack(anchor = 'w', padx = 10, pady = 10)

if __name__ == '__main__':
    root = Tkinter.Tk()
    Pmw.initialise(root)
    widget = Demo(root)
    root.mainloop()



ComboBox
Tkinter中的OptionMenu和ComboBox
#coding:utf-8
import sys
import Tkinter
import Pmw

class Demo:
    def __init__(self, parent):
        parent.configure(background = 'white')

    	self.target = Tkinter.Label(
                parent,
                relief = 'sunken',
                padx = 20,
                pady = 20,
        )
    	self.target.pack(fill = 'x', padx = 8, pady = 8)

        colours = ('cornsilk1', 'snow1', 'seashell1', 'antiquewhite1',
                    'bisque1', 'peachpuff1', 'navajowhite1', 'lemonchiffon1',
                    'ivory1', 'honeydew1', 'lavenderblush1', 'mistyrose1')

        dropdown = Pmw.ComboBox(
            	parent,
                label_text = 'Dropdown ComboBox:',
            	labelpos = 'nw',
                selectioncommand = self.changeColour,
            	scrolledlist_items = colours,
        )
        dropdown.pack(side = 'left', anchor = 'n',
                    fill = 'x', expand = 1, padx = 8, pady = 8)

        first = colours[0]
        dropdown.selectitem(first)
        self.changeColour(first)

    def changeColour(self, colour):
        print 'Colour: ' + colour
        self.target.configure(background = colour)


if __name__ == '__main__':
    root = Tkinter.Tk()
    Pmw.initialise(root)
    widget = Demo(root)
    root.mainloop()

你可能感兴趣的:(.net,python)