我在通过按钮命令通过类函数调用更新标签文本时遇到问题。我已将根帧分为五个子帧。一旦调用按钮命令功能,并要求用户输入文件。在根框中显示的用户输入文件路径,而不是从中调用功能的子帧。enter image description here
import tkinter as tk
from tkinter import *
from tkinter import filedialog as fd
from tkinter import ttk
LARGE_FONT = ('Verdana', 10)
def donothing():
print('Do Nothing')
class rootframe(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "ABC")
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 = {}
frame = StartPage(container, self)
self.frames[StartPage] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(StartPage)
for F in (StartPage, graph, piano):
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()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
sidebar = Frame(self, bg='whitesmoke', width=250, height=780,
borderwidth=2, relief='ridge')
statusbar = Frame(self, bg='whitesmoke', width=1280, height=30,
borderwidth=2, relief="ridge")
toolbar = Frame(self, bg='whitesmoke', width=1280, height=30,
borderwidth=2, relief='ridge')
helpbar = Frame(self, bg='whitesmoke', width=1280, height=150,
borderwidth=2, relief='ridge')
mainframe = Frame(self, bg='whitesmoke', width=1280, height=550,
borderwidth=2, relief="sunken")
sidebar.pack(side='left', anchor='sw')
statusbar.pack(side='bottom', anchor='center')
toolbar.pack(fill=X, side='top', anchor='center')
helpbar.pack(side='bottom', anchor='center')
mainframe.pack(side='top', anchor='center')
label = tk.Label(toolbar, text='Start Page', font=LARGE_FONT)
label.pack(side='left', anchor='sw', expand=True)
button2 = ttk.Button(toolbar, text='File Merge Tool',
command=lambda: controller.show_frame(piano))
button2.pack(side='left', expand=True)
button3 = ttk.Button(toolbar, text='Graph Page',
command=lambda: controller.show_frame(graph))
button3.pack(side='left', expand=True)
class piano(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = parent
filename = " "
sidebar = Frame(self, bg='whitesmoke', width=250, height=780, borderwidth=2,
relief='ridge')
statusbar = Frame(self, bg='whitesmoke', width=1280, height=30,
borderwidth=2, relief="ridge")
toolbar = Frame(self, bg='whitesmoke', width=1280, height=30, borderwidth=2,
relief='ridge')
helpbar = Frame(self, bg='whitesmoke', width=1280, height=150,
borderwidth=2, relief='ridge')
mainframe = Frame(self, bg='whitesmoke', width=1280, height=550,
borderwidth=2, relief="sunken")
sidebar.pack(side='left', anchor='sw')
statusbar.pack(side='bottom', anchor='center')
toolbar.pack(fill=X, side='top', anchor='center')
helpbar.pack(side='bottom', anchor='center')
mainframe.pack(side='top', anchor='center')
button2 = ttk.Button(toolbar, text='HOME',
command=lambda: controller.show_frame(StartPage))
button2.pack(side='right', anchor='nw', expand=True)
label = tk.Label(mainframe, text='File Merge', font=36)
label.place(x=510, y=10)
label1 = tk.Label(mainframe, text='file1', font=LARGE_FONT)
label1.place(x=0, y=60)
inp1 = ttk.Button(mainframe, text='Browse',
command=lambda: piano.readfile())
inp1.place(x=800, y=60)
pathlabel = Label(mainframe, background='white', width=80, relief='sunken',
borderwidth=4)
pathlabel.place(x=220, y=60)
pathlabel.config(text=filename)
label2 = tk.Label(mainframe, text='file2', font=LARGE_FONT)
label2.place(x=0, y=80)
inp2 = ttk.Button(mainframe, text='Browse',
command=lambda: piano.readfile())
inp2.place(x=800, y=80)
pathlabel1 = Label(mainframe, background='white', width=80, relief='sunken',
borderwidth=4)
pathlabel1.place(x=220, y=80)
pathlabel1.config(text=filename)
def readfile():
filename = fd.askopenfilenames(filetypes=[("TXT Files", "*.txt")])
pathlabel = Label(background='white', width=80, relief='sunken',
borderwidth=4)
pathlabel.place(x=220, y=60)
pathlabel.config(text=filename)
class graph(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text='Graph', font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = ttk.Button(self, text='Back to Home',
command=lambda: controller.show_frame(StartPage))
button1.pack()
app = rootframe()
app.mainloop()
答案 0 :(得分:0)
将pathlabel
更改为self.pathlabel
,并让函数readfile
更新pathlabel
的文本,而不是每次都创建一个新的小部件。
class piano(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller = parent
filename=" "
sidebar = Frame(self, bg='whitesmoke',width=250, height=780,borderwidth=2,relief='ridge')
statusbar = Frame(self, bg='whitesmoke',width=1280, height=30,borderwidth=2, relief="ridge")
toolbar = Frame(self, bg='whitesmoke', width=1280, height=30,borderwidth=2,relief='ridge')
helpbar = Frame(self, bg='whitesmoke', width=1280, height=150,borderwidth=2,relief='ridge')
mainframe = Frame(self, bg='whitesmoke', width=1280, height=550,borderwidth=2,relief="sunken")
sidebar.pack(side='left',anchor='sw')
statusbar.pack(side='bottom',anchor='center')
toolbar.pack(fill=X,side='top',anchor='center')
helpbar.pack(side='bottom',anchor='center')
mainframe.pack(side='top',anchor='center')
button2=ttk.Button(toolbar,text='HOME',command=lambda:controller.show_frame(StartPage))
button2.pack(side='right',anchor='nw',expand=True)
label=tk.Label(mainframe,text='File Merge',font=36)
label.place(x=510,y=10)
label1=tk.Label(mainframe,text='file1',font=LARGE_FONT)
label1.place(x=0,y=60)
inp1=ttk.Button(mainframe,text='Browse',command=lambda: self.readfile(self.pathlabel))
inp1.place(x=800,y=60)
self.pathlabel = Label(mainframe,background='white',width=80,relief='sunken',borderwidth=4)
self.pathlabel.place(x=220,y=60)
self.pathlabel.config(text=filename)
label2=tk.Label(mainframe,text='file2',font=LARGE_FONT)
label2.place(x=0,y=80)
inp2=ttk.Button(mainframe,text='Browse',command=lambda: self.readfile(self.pathlabel1))
inp2.place(x=800,y=80)
self.pathlabel1 = Label(mainframe,background='white',width=80,relief='sunken',borderwidth=4)
self.pathlabel1.place(x=220,y=80)
self.pathlabel1.config(text=filename)
def readfile(self,path_entry):
filename = fd.askopenfilenames(filetypes=[("TXT Files", "*.txt")])
path_entry.config(text=filename)