我正在尝试开发一个程序,以帮助我在学校的工作,并在此方面取得了很大的进步,但是现在我很混乱,我需要对如何使文件路径显示在Entry小部件上进行罚款。我在这里尝试了很多东西,但我可能没有理解它背后的想法,请帮助我
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror
from tkinter import ttk
import xlrd
class SchoolProjict(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.app_data = {"name": tk.StringVar(),
}
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (StartPage, SetingPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def get_page(self, classname):
for page in self.frames.values():
if str(page.__class__.__name__) == classname:
return page
return None
def printingstuff(var1):
print (var1)
def printontherthing(page_class):
print(page_class)
class StartPage(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
tk.Frame.__init__(self, parent)
lablel = ttk.Label(self, text = "Main Page")
lablel.pack(pady = 10, padx = 10)
button2 = ttk.Button(self, text = "Siting", command = lambda: controller.show_frame(SetingPage))
button2.pack()
class SetingPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
lablel = tk.Label(self, text = "Siting Page")
lablel.grid(row = 0, column = 0)
text1 = tk.Entry(self) #<== i want to show the path of the file i am going to open Here after i select it from openfile
text1.grid(row = 2, column = 0)
text1.focus()
button1 = tk.Button(self, text = "print text1", command = lambda: printingstuff(text1.get()))
button1.grid(row = 3, column = 0)
button2 = tk.Button(self, text="open file", command= self.load_file, width=10)
button2.grid(row = 3, column = 1)
button4 = tk.Button(self, text = "Main Page", command = lambda: controller.show_frame(StartPage))
button4.grid(row = 4, column = 1)
def load_file(self):
fname = askopenfilename(filetypes=(("Excel file", "*.xls"),
("HTML files", "*.html;*.htm"),
("All files", "*.*") ))
if fname:
try:
# print(fname)
value = str(fname)
page_var = self.controller.get_page("SetingPage")
page_var.text1.insert(0, value)
return
except:
showerror("Open Source File", "Failed to read file\n'%s'" % fname)
return
app = SchoolProjict()
app.mainloop()
它给了我一个错误,我运行它并选择文件 this is what i have as an error
答案 0 :(得分:0)
您需要通过添加text
使SetingPage
成为类self
的属性。
class SetingPage(tk.Frame):
def __init__(self, parent, controller):
...
self.text1 = tk.Entry(self) #<== i want to show the path of the file i am going to open Here after i select it from openfile
self.text1.grid(row = 2, column = 0)
self.text1.focus()
button1 = tk.Button(self, text = "print text1", command = lambda: printingstuff(self.text1.get()))
...