我的程序是这样的..从tkinter导入中将tkinter作为tk导入*
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
self.title(" Allocation")
width, height = self.winfo_screenwidth(), self.winfo_screenheight()
self.geometry('%dx%d+0+0' % (width,height))
self.state('zoomed')
self.wm_iconbitmap('icon.ico')
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, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, c):
frame = self.frames[c]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
logo = tk.PhotoImage(file="backesh.ppm")
BGlabel = tk.Label(self,image=logo)
BGlabel.image = logo
BGlabel.place(x=0,y=0,width=592,height=450)
label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
frame1 = Frame(self)
Label(frame1, bd=5,bg="black",text=" Enter text : ",font=("Helvetica", 14),fg="light green",width=21).pack(side=LEFT)
emp=Entry(frame1, bd =5,font=("Times New Roman", 14),width=25)
emp.pack(side=LEFT)
frame1.place(x=400,y=160)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
button2 = tk.Button(self, text="Go to Page two",
command=lambda: controller.show_frame(PageTwo))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button2.place(x=300,y=406,width=200,height=44)
button3.place(x=500,y=406,width=80,height=44)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
logo = tk.PhotoImage(file="backesh.ppm")
BGlabel = tk.Label(self,image=logo)
BGlabel.image = logo
BGlabel.place(x=0,y=0,width=592,height=450)
label = tk.Label(self, text="This is page one", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
button1 = tk.Button(self, text="Go to Start Page",
command=lambda: controller.show_frame(StartPage))
#button2 = tk.Button(self, text="Go to Page two",
# command=lambda: controller.show_frame(PageTwo))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button3.place(x=300,y=406,width=200,height=44)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
logo = tk.PhotoImage(file="backesh.ppm")
BGlabel = tk.Label(self,image=logo)
BGlabel.image = logo
BGlabel.place(x=0,y=0,width=592,height=450)
label = tk.Label(self, text="This is page two", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
button1 = tk.Button(self, text="Go to Start Page",
command=lambda: controller.show_frame(StartPage))
#button2 = tk.Button(self, text="Go to Page two",
# command=lambda: controller.show_frame(PageTwo))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button3.place(x=300,y=406,width=200,height=44)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
我想从StartPage中获取输入文本数据,并将其显示为PageOne中的标签。怎么做?我是新来的。请输入验证码。提前致谢。
首先,更正PageOne和StartPage类的代码,并将self.controller = controller添加到__init__函数中:
class PageOne(tk.Frame):
def __init__(self, parent, controller):
#your code
self.controler = controller
在StartPage的输入字段之前和PageOne的标签之前添加self:
#entry in StartPage
self.emp=tk.Entry(frame1, bd =5,font=("Times New Roman", 14),width=25)
self.emp.pack(side=tk.LEFT)
#label in PageOne
self.label = tk.Label(self, text="This is page one", font=TITLE_FONT)
self.label.place(x=0,y=0,width=592,height=44)
然后将函数go_to_page_one添加到StartPage类:
def go_to_page_one(self):
self.controller.SomeVar = self.emp.get() #save text from entry to some var
self.controller.frames[PageOne].correct_label() #call correct_label function
self.controller.show_frame(PageOne) #show page one
在StartPage类的button1上,将命令更改为lambda:self.go_to_page_one():
button1 = tk.Button(self, text="Go to Page One",
command=lambda: self.go_to_page_one())
最后,将正确的功能标签添加到类PageOne中:
def correct_label(self):
self.label.config(text=self.controller.SomeVar) #correct the label
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句