我试图在tkinter中编写一个程序,用户点击一个带有其名称的按钮,然后验证页面会向他们显示该程序。我遇到的问题是变量要么重置,要么我错误地访问它:
import tkinter as tk
from tkinter import *
from tkinter import ttk
LARGE_FONT = ("Times New Roman", 12)
NORM_FONT = ("Times New Roman", 10)
root = Tk()
root.withdraw()
class DIS(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="")
tk.Tk.wm_title(self, "program")
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, contactQues, nameVerify):
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)
button2 = ttk.Button(self, text = "Name Select",
command=lambda: controller.show_frame(contactQues))
button2.pack()
class contactQues(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
global name
name = StringVar()
label1 = tk.Label(self, text = "Select Your Name", font = LARGE_FONT)
label1.pack(pady=10, padx=10)
button2 = ttk.Button(self, text = "Bojangles", command = self.bojangles)
button2.pack(pady=5)
def bojangles(self):
name.set("Mr. Bojangles")
self.controller.show_frame(nameVerify)
#
#Many other names to select
#
class nameVerify(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
namename = name.get()
label5 = tk.Label(self, text = "Your Name:", font = LARGE_FONT)
label5.pack(pady=10, padx=10)
labelcontact = tk.Label(self, text = namename, font = NORM_FONT)
labelcontact.pack()
app = DIS()
app.mainloop()
所以,从本质上讲,我想要发生的是: - 程序运行和用户按下"名称选择",用户选择他们的名字,最后一页显示他们的选择。
我已经尝试过使用全局变量,labelcontact
标签的文本变量,StringVar()等,而且似乎无法将这个变为混乱。
有更好的方法吗?或者我在做一些内在错误的事情?
感谢您的帮助。
答案 0 :(得分:1)
我建议将name
作为DIS
类的属性。然后,您的StartPage和nameVerify实例可以通过其controller
属性访问它。如果您希望labelcontact
在name
时自动更新,请使用textvariable
属性。
此外,您还需要删除root = Tk()
和root.withdraw()
行。我不知道为什么,但只要他们在那里,标签联系标签就不会正确更新。在任何情况下,他们似乎都不做任何事情 - 希望它们对您的实际代码并不重要。
import tkinter as tk
from tkinter import *
from tkinter import ttk
LARGE_FONT = ("Times New Roman", 12)
NORM_FONT = ("Times New Roman", 10)
class DIS(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="")
tk.Tk.wm_title(self, "program")
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.name = StringVar()
self.frames = {}
for F in (StartPage, contactQues, nameVerify):
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)
button2 = ttk.Button(self, text = "Name Select",
command=lambda: controller.show_frame(contactQues))
button2.pack()
class contactQues(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label1 = tk.Label(self, text = "Select Your Name", font = LARGE_FONT)
label1.pack(pady=10, padx=10)
button2 = ttk.Button(self, text = "Bojangles", command = self.bojangles)
button2.pack(pady=5)
def bojangles(self):
self.controller.name.set("Mr. Bojangles")
self.controller.show_frame(nameVerify)
#
#Many other names to select
#
class nameVerify(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label5 = tk.Label(self, text = "Your Name:", font = LARGE_FONT)
label5.pack(pady=10, padx=10)
labelcontact = tk.Label(self, textvariable = self.controller.name, font = NORM_FONT)
labelcontact.pack()
app = DIS()
app.mainloop()