我的python
脚本正在使用下面的代码,但是tkinter
不能逐行打印,我怀疑函数内部的函数不起作用
工作脚本:
def writeline(line):
down.write(line + "\n")
ip="**.**.**.**"
ftp = FTP(ip)
ftp.login('','')
print ("PLEASE ENTER THE PROGRAM NAME TO BE EXECUTED :")
file = input()
try:
down = open(file, "w")
print(ftp.voidcmd( "site file=jes" ))
print(ftp.voidcmd( "site sbd=(IBM-273,ISO8859-1)"))
ftp.retrlines("retr " + file, writeline)
down.close()
except:
print("Unable to Download file")
Tkinter
代码(不起作用):
def writeline(line):
down.write(line + "\n")
def downloadFile():
file = ent_input.get()
try:
down = open(file, "w")
writeline()
ftp.voidcmd("site file=jes")
ftp.voidcmd("site sbd=(IBM-273,ISO8859-1)")
text_servermsg.insert(END,"Downloading " + file + " ...")
text_servermsg.insert(END,ftp.retrlines("retr " + file ,writeline))
except:
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,"Unable to Download file")
完整代码(更新)
import tkinter
from tkinter import *
import ftplib
import os
from ftplib import FTP
ftp = ftplib.FTP()
def connectServer():
ip = " "
try:
msg = ftp.connect(ip)
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,msg)
lbl_login.place(x=150,y=20)
ent_login.place(x=150,y=40)
lbl_pass.place(x=150,y=60)
ent_pass.place(x=150,y=80)
btn_login.place(x=182,y=110)
except:
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,"Unable to connect")
def loginServer():
user = " "
password = " "
try:
msg = ftp.login(user,password)
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,msg)
displayDir()
except:
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,"Unable to login")
def displayDir():
libox_serverdir.insert(0,"--------------------------------------------")
dirlist = []
dirlist = ftp.nlst()
for item in dirlist:
libox_serverdir.insert(0, item)
#FTP commands
def writeline(line):
down.write(line + "\n")
def downloadFile():
file = ent_input.get()
try:
down = open(file, "wt")
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,ftp.voidcmd("SITE FILETYPE=JES"))
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,ftp.voidcmd("site sbd=(IBM-273,ISO8859-1)"))
text_servermsg.insert(END,"\n")
writeline(line)
text_servermsg.insert(END,"Downloading " + file + " ...")
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,ftp.retrlines("RETR %s" % file, writeline))
except:
text_servermsg.insert(END,"\n")
text_servermsg.insert(END,"Unable to Download file")
window = tkinter.Tk()
window.title("Hari's Mainframe Connect")
window.geometry("1000x650")
#Connect
lbl_ip = tkinter.Label(window, text="IP Address")
ent_ip = tkinter.Entry(window)
btn_connect = tkinter.Button(window, text="Connect", command=connectServer)
#Server response text box
text_servermsg = tkinter.Text(window,width=80,height=26)
#Login
lbl_login = tkinter.Label(window, text="Username")
ent_login = tkinter.Entry(window)
lbl_pass = tkinter.Label(window, text="Password")
ent_pass = tkinter.Entry(window)
btn_login = tkinter.Button(window, text="Login", command=loginServer)
#Directory listing
lbl_dir = tkinter.Label(window, text="Directory listing:")
libox_serverdir = tkinter.Listbox(window,width=40,height=14)
#Options
lbl_input = tkinter.Label(window, text="Input")
ent_input = tkinter.Entry(window)
btn_downfile = tkinter.Button(window, text="Download File", command=downloadFile,width=15)
#Place widgits
lbl_ip.place(x=20,y=20)
ent_ip.place(x=20,y=40)
btn_connect.place(x=52,y=110)
text_servermsg.place(x=20,y=150)
lbl_dir.place(x=700,y=143)
libox_serverdir.place(x=700,y=165)
lbl_input.place(x=700,y=400)
ent_input.place(x=700,y=420)
btn_downfile.place(x=850,y=450)
#Create
window.mainloop()