[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Example 8-16. PP4E\Gui\tour\message.py


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()


Figure 8-21. A Message widget at work


Entry


The Entry widget is a simple, single-line text input field. It is typically used for input
fields in form-like dialogs and anywhere else you need the user to type a value into a
field of a larger display. Entry also supports advanced concepts such as scrolling, key
bindings for editing, and text selections, but it’s simple to use in practice. Exam-
ple 8-17 builds the input window shown in Figure 8-22.


Example 8-17. PP4E\Gui\tour\entry1.py


from tkinter import *
from quitter import Quitter


def fetch():
print('Input => "%s"' % ent.get()) # get text


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('', (lambda event: fetch())) # on enter key
btn = Button(root, text='Fetch', command=fetch) # and on button
btn.pack(side=LEFT)
Quitter(root).pack(side=RIGHT)
root.mainloop()


Message and Entry | 449
Free download pdf