| 
 | 
 
 
 楼主 |
发表于 2021-1-5 11:36:34
|
显示全部楼层
 
 
 
 本帖最后由 Decemer 于 2021-1-5 11:52 编辑  
 
 
为啥你这样就可以,我这样就不行,直接报text没定义 
- from tkinter import *
 
 - import tkinter.filedialog
 
  
- def openfile():
 
 -     filename = tkinter.filedialog.askopenfilename(title='选择txt文件', filetypes=[('TXT', '*.txt'), ('All Files', '*')])
 
 -     print(filename)
 
 -     with open (str(filename),"r+") as myfile:
 
 -         source = myfile.read()
 
 -         print(source)
 
 -         exec(source)
 
 -         print(text)
 
  
- root = Tk()
 
 - root.title("text")
 
  
- frame = Frame(root)
 
 - frame.pack()
 
 - read = StringVar()
 
 - readEntry = Entry(frame,textvariable=read)
 
 - openButton = Button(frame,text="open",command=openfile)
 
 - readEntry.grid(row=1,column=1)
 
 - openButton.grid(row=1,column=2)
 
 - #txt = Text(root)
 
 - #txt.pack()
 
  
- root.mainloop()
 
  复制代码 
 
试了一下,用 
- with open ("t.txt") as myfile:
 
  复制代码 
是可以获取到text的值,但是用 
- with open (str(filename)) as myfile:
 
  复制代码 
就获取不到,不知道是因为啥。。。 |   
 
 
 
 |