关于 Tkinter 的问题:
我想创建一个浏览以及一个文本显示,它将显示我从浏览按钮中选择的文件。以下是我的代码:
编辑 1。
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
Tkinter.Button(self, text='Browse and open filename - then manually upload it', command=self.askopenfilename).pack(**button_opt)
self.file_opt = options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
options['initialdir'] = 'C:\\'
options['initialfile'] = 'myfile.txt'
options['parent'] = root
options['title'] = 'Browse'
self.dir_opt = options = {}
options['initialdir'] = 'C:\\'
options['mustexist'] = False
options['parent'] = root
options['title'] = 'Browse'
image = Image.open("/home/kuber/Downloads/godata/images/header.png")
photo = ImageTk.PhotoImage(image)
label = Label(image=photo)
label.image = photo # keep a reference!
label.place(width=768, height=576)
label.pack(side = TOP)
self.centerWindow()
self.master.columnconfigure(10, weight=1)
#Tkinter.Button(self, text='upload file', command=self.Fname).pack(**button_opt)
self.file_name = Text(self, width=39, height=1, wrap=WORD)
def Fname(self):
self.file_name = Text(self, width=39, height=1, wrap=WORD)
self.file_name.grid(row=1, column=1, columnspan=4, sticky=W)
def askopenfilename(self):
# get filename
filename = tkFileDialog.askopenfilename(**self.file_opt)
# open file on your own
if filename:
with open(self.filename, 'r') as inp_file:
print "1"
self.file_name.delete(0.0, END)
self.file_name.insert(0.0, inp_file.read())
#return open(filename, 'r')
当我按下浏览按钮并打开文件时。我希望从askopenfilename
功能转到文本小部件。但我得到错误:
AttributeError: TkFileDialogExample instance has no attribute 'filename'
此外,当我在 Fname 之外包含 self.file_name.grid(row=1, column=1, columnspan=4,sticky=W) 时,Tkinter 挂起。