to do: use threads, show download status and getfile prints;
"""
from form import Form
from tkinter import Tk, mainloop
from tkinter.messagebox import showinfo
import getfile, os
class GetfileForm(Form):
def init(self, oneshot=False):
root = Tk()
root.title('getfilegui')
labels = ['Server Name', 'Port Number', 'File Name', 'Local Dir?']
Form.init(self, labels, root)
self.oneshot = oneshot
def onSubmit(self):
Form.onSubmit(self)
localdir = self.content['Local Dir?'].get()
portnumber = self.content['Port Number'].get()
servername = self.content['Server Name'].get()
filename = self.content['File Name'].get()
if localdir:
os.chdir(localdir)
portnumber = int(portnumber)
getfile.client(servername, portnumber, filename)
showinfo('getfilegui', 'Download complete')
if self.oneshot: Tk().quit() # else stay in last localdir
if name == 'main':
GetfileForm()
mainloop()
The form layout class imported here can be used by any program that needs to input
form-like data; when used in this script, we get a user interface like that shown in
Figure 12-6 under Windows 7 (and similar on other versions and platforms).
Figure 12-6. getfilegui in action
Pressing this form’s Submit button or the Enter key makes the getfilegui script call
the imported getfile.client client-side function as before. This time, though, we also
A Simple Python File Server | 849