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

(yzsuai) #1

Figure 12-2. getfilegui-1 verification pop up


Using grids and function calls


The first user-interface script (Example 12-18) uses the pack geometry manager and
row Frames with fixed-width labels to lay out the input form and runs the getfile client
as a standalone program. As we learned in Chapter 9, it’s arguably just as easy to use
the grid manager for layout and to import and call the client-side logic function instead
of running a program. The script in Example 12-19 shows how.


Example 12-19. PP4E\Internet\Sockets\getfilegui-2.py


"""
same, but with grids and import+call, not packs and cmdline;
direct function calls are usually faster than running files;
"""


import getfile
from tkinter import *
from tkinter.messagebox import showinfo


def onSubmit():
getfile.client(content['Server'].get(),
int(content['Port'].get()),
content['File'].get())
showinfo('getfilegui-2', 'Download complete')


box = Tk()
labels = ['Server', 'Port', 'File']
rownum = 0
content = {}
for label in labels:
Label(box, text=label).grid(column=0, row=rownum)
entry = Entry(box)
entry.grid(column=1, row=rownum, sticky=E+W)
content[label] = entry
rownum += 1


box.columnconfigure(0, weight=0) # make expandable


A Simple Python File Server | 845
Free download pdf