|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想用tkinter写一个输入文件地址,点击Buttom能原样显示文件(txt)内容,选中该内容任意一列,点击保存就能生成新文件并保存的程序~
目前我的程序怎么也不显示文本内容,具体程序如下:
from tkinter import *
root=Tk()
root.title('天气预报数据处理窗口')#窗口名字
root.geometry('800x450')#设置主窗口大小
label=Label(root,text="文件名:")
label.grid(row=1,column=0)
v1=StringVar()
e1=Entry(root,textvariable=v1)
e1.grid(row=1,column=2)
v2=StringVar()
def openfile():
newwindow=Toplevel()
newwindow.title('天气预报数据',)
newwindow.geometry('640x40')
text1=Text(newwindow)
filename = str(e1.get())
try:
with open(filename) as f:
for each_line in f:
text1.insert(INSERT,each_line)
except OSError as reason:
print('文件不存在!\n请重新输入文件名'+str(reason))
b1=Button(root,text="确认",command=openfile)
b1.grid(row=2,column=2)
mainloop()
还请鱼油们指点一二~~~在此谢过了
不懂你的意思,这是修改后的代码
- from tkinter import *
- root=Tk()
- root.title('天气预报数据处理窗口')#窗口名字
- root.geometry('800x450')#设置主窗口大小
- def openfile():
- filename = str(e1.get())
- try:
- with open(filename) as f:
- for each_line in f:
- text.insert(INSERT,each_line)
- except OSError as reason:
- print('文件不存在!\n请重新输入文件名'+str(reason))
-
- topFrame = Frame(root,bd=1, relief=SUNKEN)
- topFrame.pack(fill=BOTH)
- bottomFrame = Frame(root,bd=1, relief=SUNKEN)
- bottomFrame.pack(fill=BOTH)
- label=Label(topFrame,text="文件名:")
- label.grid(row=0,column=0, sticky=W)
- v1=StringVar()
- e1=Entry(topFrame,textvariable=v1)
- e1.grid(row=0,column=1,sticky=W)
- b1=Button(topFrame,text="确认",command=openfile)
- b1.grid(row=0,column=2,padx=5,sticky=W)
- text = Text(bottomFrame, height=600)
- text.pack(fill=BOTH)
- mainloop()
复制代码
|
|