如何 Entry.insert() 在 tkinter 中使用框架?
how to Entry.insert() work with frames in tkinter?
我正在尝试制作一个程序来帮助我在学校的工作,并且我在这方面取得了很大的进步,但我现在一团糟,我需要罚款如何使文件的路径显示在输入小部件我在这里尝试了很多东西,但我可能没有 git 它背后的想法请帮助我
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()
它给了我一个错误抱怨我 运行 它和 select 文件
this is what i have as an error
您需要通过添加 self
.
使您的 text
成为 class SetingPage
的属性
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()))
...
我正在尝试制作一个程序来帮助我在学校的工作,并且我在这方面取得了很大的进步,但我现在一团糟,我需要罚款如何使文件的路径显示在输入小部件我在这里尝试了很多东西,但我可能没有 git 它背后的想法请帮助我
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()
它给了我一个错误抱怨我 运行 它和 select 文件 this is what i have as an error
您需要通过添加 self
.
text
成为 class SetingPage
的属性
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()))
...