def __init__(self, parent=None, **options): Frame.__init__(self, parent, **options) self.pack() Label(self, text='Scale demos').pack() self.var = IntVar() # 两个Scale对象设置同样的variable,滑动一个标尺,另一个标尺位置自动变化 Scale(self, label='Pick demo number', command=self.onMove, variable=self.var, from_=0, to=len(demos) - 1).pack() Scale( self, label='Pick demo number', command=self.onMove, variable=self.var, # command 对应的回调函数会自动获取对象值作为参数 from_=0, to=len(demos) - 1, length=200, tickinterval=1, showvalue=YES, orient='horizontal').pack() Quitter(self).pack(side=LEFT) Button(self, text='Run demo', command=self.onRun).pack(side=LEFT) Button(self, text='State', command=self.report).pack(side=RIGHT)
def __init__(self, parent=None): Frame.__init__(self, parent) self.pack() Label(self, text='Basic Demos').pack() for key in demos: func = (lambda key=key: self.printit(key)) # 使用 lambda 函数传递回调数据 Button(self, text=key, command=func).pack(side=TOP, fill=BOTH) Quitter(self).pack(side=TOP, fill=BOTH)
def __init__(self, parent=None, file=None): frm = Frame(parent) frm.pack(fill=X) Button(frm, text='Save', command=self.onSave).pack(side=LEFT) Button(frm, text='Cut', command=self.onCut).pack(side=LEFT) Button(frm, text='Paste', command=self.onPaste).pack(side=LEFT) Button(frm, text='Find', command=self.onFind).pack(side=LEFT) Quitter(frm).pack(side=LEFT) ScrolledText.__init__(self, parent, file=file) self.text.config(font=('courier', 12, 'normal'))
def __init__(self, parent=None, **options): Frame.__init__(self, parent, **options) self.pack() Label(self, text='Radio demos').pack(side=TOP) self.var = StringVar() for key in demos: Radiobutton(self, text=key, command=self.onPress, variable=self.var, value=key).pack(anchor=NW) # 每个单选按钮设定了不同的value值,但关联了同一个变量 self.var # 哪个按钮被选中,共享变量self.var的值即变为该按钮的value self.var.set(key) Button(self, text='State', command=self.report).pack(fill=X) Quitter(self).pack(fill=X)
#!python3 # entry1.py - from tkinter import * from dialogTable import Quitter def fetch(): # 获取文本输入框类文本 print('Input =>"%s"' % ent.get()) root = Tk() ent = Entry(root, show='*') ent.insert(0, 'Type words here') ent.pack(side=TOP, fill=X) ent.focus() # 设置得出输入字段的输入焦点,可以使用户在输入前不必单击输入框 ent.bind('<Return>', (lambda event: fetch())) btn = Button(root, text='Fetch', command=fetch) btn.pack(side=LEFT) Quitter(root).pack(side=RIGHT) root.mainloop()
from dialogTable import Quitter demoMudules = ['dialogTable', 'demoCheck', 'demoRadio', 'demoScale'] parts = [] def addComponents(root): for demo in demoMudules: module = __import__(demo) part = module.Demo(root) part.config(bd=2, relief=GROOVE) part.pack(side=LEFT, fill=BOTH, expand=YES) parts.append(part) def dumpState(): for part in parts: print(part.__module__ + ':', end=' ') if hasattr(part, 'report'): part.report() else: print('none') root = Tk() root.title('Frames') Label(root, text='Multipile Frame demo', bg='white').pack() Button(root, text='State', command=dumpState).pack(fill=X) Quitter(root).pack(fill=X) addComponents(root) root.mainloop()
def tools(self): frm = Frame(self) frm.pack(side=RIGHT) Button(frm, text='State', command=self.report).pack(fill=X) Quitter(frm).pack(fill=X)