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 插入值
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()
时间: 2024-10-13 11:16:28