7tkinter笔记五
Message and Entry组件
from tkinter import * msg = Message(text="Oh by the way, which one's Pink?") msg.config(bg='pink', font=('times', 16, 'italic')) msg.pack(fill=X, expand=YES) mainloop()
from tkinter import * import sys def fetch(): print('Input => "%s"' % ent.get()) # get text def quit(): sys.exit() root = Tk() ent = Entry(root) ent.insert(0, 'Type words here') # set text 插入一个原始默认的文本 ent.pack(side=TOP, fill=X) # grow horiz 获得水平增大 ent.focus() # save a click 保存单击事件 ent.bind('<Return>', (lambda event: fetch())) # on enter key 当按下回车键时触发 btn = Button(root, text='Fetch', command=fetch) # and on button 当按下按钮时触发 btn.pack(side=LEFT) btn1 = Button(root,text='QUIT',command=quit) btn1.pack(side=RIGHT) root.mainloop()
ent.insert(0, 'some text') # set value 保存值 value = ent.get() # fetch value (a string) 获取值 ent.delete(0, END) # first, delete from start to end 清空值 ent.insert(0, 'some text') # then set value 插入值
表单
from tkinter import * import sys fields = 'Name', 'Job', 'Pay' def fetch(variables): for variable in variables: print('Input => "%s"' % variable.get()) # get from var def quit(): sys.exit() def makeform(root, fields): form = Frame(root) # make outer frame left = Frame(form) # make two columns rite = Frame(form) form.pack(fill=X) left.pack(side=LEFT) rite.pack(side=RIGHT, expand=YES, fill=X) # grow horizontal variables = [] for field in fields: lab = Label(left, width=5, text=field) # add to columns ent = Entry(rite) lab.pack(side=TOP) ent.pack(side=TOP, fill=X) # grow horizontal var = StringVar() ent.config(textvariable=var) # link field to var var.set('enter here') variables.append(var) return variables if __name__ == '__main__': root = Tk() vars = makeform(root, fields) Button(root, text='Fetch', command=(lambda: fetch(vars))).pack(side=LEFT) Button(root,text='QUIT',command=quit).pack(side=RIGHT) root.bind('<Return>', (lambda event: fetch(vars))) root.mainloop()
复选按钮
from tkinter import * states = [] # change object not name def onPress(i): # keep track of states states[i] = not states[i] # changes False->True, True->False root = Tk() for i in range(10): chk = Checkbutton(root, text=str(i), command=(lambda i=i: onPress(i)) ) chk.pack(side=LEFT) states.append(False) root.mainloop() print(states) # show all states on exit
单选按钮
from tkinter import * root = Tk() var = StringVar() for i in range(10): rad = Radiobutton(root, text=str(i), variable=var, value=str(i % 3)) rad.pack(side=LEFT) var.set(' ') # deselect all initially root.mainloop()
from tkinter import * state = '' buttons = [] def onPress(i): global state state = i #print(buttons) for btn in buttons: btn.deselect() buttons[i].select() root = Tk() for i in range(10): rad = Radiobutton(root, text=str(i), value=str(i), command=(lambda i=i: onPress(i)) ) rad.pack(side=LEFT) buttons.append(rad) onPress(0) # select first initially root.mainloop() print(state) # show state on exit
标尺
from tkinter import * root = Tk() scl = Scale(root, from_=-100, to=100, tickinterval=50, resolution=10) scl.pack(expand=YES, fill=Y) def report(): print(scl.get()) Button(root, text='state', command=report).pack(side=RIGHT) root.mainloop()
resolution=10 每次移动10个单位
tickinterval=50 间隔为50个单位
from tkinter import * class Checkbar(Frame): def __init__(self, parent=None, picks=[], side=LEFT, anchor=W): Frame.__init__(self, parent) self.vars = [] for pick in picks: var = IntVar() chk = Checkbutton(self, text=pick, variable=var) chk.pack(side=side, anchor=anchor, expand=YES) self.vars.append(var) def state(self): return [var.get() for var in self.vars] class Radiobar(Frame): def __init__(self, parent=None, picks=[], side=LEFT, anchor=W): Frame.__init__(self, parent) self.var = StringVar() self.var.set(picks[0]) for pick in picks: rad = Radiobutton(self, text=pick, value=pick, variable=self.var) rad.pack(side=side, anchor=anchor, expand=YES) def state(self): return self.var.get() if __name__ == '__main__': root = Tk() lng = Checkbar(root, ['Python', 'C#', 'Java', 'C++']) gui = Radiobar(root, ['win', 'x11', 'mac'], side=TOP, anchor=NW) tgl = Checkbar(root, ['All']) gui.pack(side=LEFT, fill=Y) lng.pack(side=TOP, fill=X) tgl.pack(side=LEFT) lng.config(relief=GROOVE, bd=2) gui.config(relief=RIDGE, bd=2) def allstates(): print(gui.state(),lng.state()) #print(gui.state(), lng.state(), tgl.state()) 分别显示 win..,python...,all三个状态 Button(root, text='Peek', command=allstates).pack(side=RIGHT) root.mainloop()
v
bc
用entry设计表单
from tkinter import * fields = 'Name', 'Job', 'Pay' def fetch(entries): for entry in entries: print('Input => "%s"' % entry.get()) # get text def quit(): sys.exit() def makeform(root, fields): entries = [] for field in fields: row = Frame(root) # make a new row lab = Label(row, width=5, text=field) # add label, entry ent = Entry(row) row.pack(side=TOP, fill=X) # pack row on top lab.pack(side=LEFT) ent.pack(side=RIGHT, expand=YES, fill=X) # grow horizontal entries.append(ent) return entries if __name__ == '__main__': root = Tk() ents = makeform(root, fields) root.bind('<Return>', (lambda event: fetch(ents))) Button(root, text='Fetch',command= (lambda: fetch(ents))).pack(side=LEFT) Button(root,text='QUIT',command=quit).pack(side=RIGHT) root.mainloop()