(tkinter)为什么不使用标签显示我的变量?

时间:2018-08-09 12:33:34

标签: python tkinter global display tkinter-entry

我正在尝试编写一个程序,该程序在一个页面上接收用户信息,例如其姓名和职务,然后在另一页面上显示这些条目。我正在使用Tkinter,但无法使他们的条目显示在另一页上。这是程序:

import tkinter as tk
from tkinter import ttk

#PROFILE VARS
FirstName = ('none')

#INITIALIZING
class MegaQuiz(tk.Tk): 

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "THE MEGA POP QUIZ")

        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 (ProfilePage, MainPage):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("ProfilePage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

#PROFILE PAGE
class ProfilePage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        global FirstName

        #Profile Title
        profile_title = tk.Label(self, text="Create A Profile Yo")
        profile_title.grid(column=0, row=2)

        #FIRST NAME
        Q1_title = tk.Label(self, text="First Name:")
        Q1_title.grid(column=0, row=1)

        FirstNameEntry = tk.Entry(self)
        FirstNameEntry.grid(column=2, row=4)
        FirstName = str(FirstNameEntry.get())


        #NEXT BUTTON
        Button1 = tk.Button(self, text="NEXT",
                        command = lambda: controller.show_frame("MainPage"))
        Button1.grid(column=10, row=10)

#MAIN MENU PAGE
class MainPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        #Play BUTTON
        PlayBTN = tk.Button(self, text="PLAY", width=40)
        PlayBTN.grid(column=0, row=20, sticky="nesw")

        #ProfileDisplay
        FirstNameDis = tk.Label(self, text=('FirstName: ' + FirstName))
        FirstNameDis.grid(column=0, row=0, sticky="w")

#RUNNING PROGRAM
app = MegaQuiz()
app.mainloop()

问题是它显示“名字:”,但不显示变量FirstName,仅显示空白,帮助。

1 个答案:

答案 0 :(得分:0)

解决此问题的最快方法是将FirstName变量作为类属性移至MegaQuiz,然后从那里使用它。我们可以更改您的NEXT按钮命令以调用类方法,然后从该方法更新FirstName变量,然后再从同一方法设置FirstNameDis标签文本。

首先通过将这一行放在FirstName部分中,将MegaQuiz变量移至__init__类中。

self.FirstName = 'none'

然后更改此:

#NEXT BUTTON
Button1 = tk.Button(self, text="NEXT", command = lambda: controller.show_frame("MainPage"))

对此:

    #NEXT BUTTON
    Button1 = tk.Button(self, text="NEXT", command=self.next_button)
    Button1.grid(column=10, row=10)

def next_button(self):
    self.controller.FirstName = self.FirstNameEntry.get()
    self.controller.frames["MainPage"].FirstNameDis.config(text='FirstName: ' + self.controller.FirstName)
    self.controller.show_frame("MainPage")

在您的MainPage中进行以下更改:

self.FirstNameDis = tk.Label(self, text='FirstName: ' + FirstName)

对此:

self.FirstNameDis = tk.Label(self, text='FirstName: ' + self.controller.FirstName)

那应该是解决此问题的全部方法。

我确实注意到了一些PEP8问题,所以这里是您对代码进行了重写,以解决您的问题,并重写一些内容以更好地遵循PEP8准则。

  1. 不确定为什么要将()添加到某些字符串变量中,但这不是必需的。
  2. 变量和类属性名称应全部小写,并在单词之间加下划线。
  3. 您不需要为所有内容提供变量名。如果您知道有按钮或标签,则以后将不会进行更新,则可以在不使用变量名的情况下编写它们。

代码:

import tkinter as tk


class MegaQuiz(tk.Tk): 

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self)
        self.title("THE MEGA POP QUIZ")
        self.first_name = 'none'
        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 (ProfilePage, MainPage):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame("ProfilePage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()


class ProfilePage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        tk.Label(self, text="Create A Profile Yo").grid(column=0, row=2)
        tk.Label(self, text="First Name: ").grid(column=0, row=1)

        self.fn_entry = tk.Entry(self)
        self.fn_entry.grid(column=2, row=4)

        tk.Button(self, text="NEXT", command=self.next_button).grid(column=10, row=10)

    def next_button(self):
        self.controller.first_name = self.fn_entry.get()
        self.controller.frames["MainPage"].fn_dis.config(text='FirstName: {}'.format(self.controller.first_name))
        self.controller.show_frame("MainPage")


class MainPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        tk.Button(self, text="PLAY", width=40).grid(column=0, row=20, sticky="nesw")
        self.fn_dis = tk.Label(self)
        self.fn_dis.grid(column=0, row=0, sticky="w")


app = MegaQuiz()
app.mainloop()