0

I'm trying to write a program which takes the users information, like their name and stuff, on one page, and then displays these entries on another. I'm using Tkinter and I can't get their entries to display on the other page. Here's the program:

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()

The problem is that it displays the "FirstName: ", but doesn't display the variables FirstName, just blankness, help.

toti08
  • 2,448
  • 5
  • 24
  • 36
Metalcat
  • 43
  • 2
  • 7
  • 2
    When you update your `FirstName` variable the entry is still empty, you should get the content after the user has inserted its name, otherwise it's empty. – toti08 Aug 09 '18 at 12:57
  • There's more information [here](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter) or [here](https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter/33650527#33650527) – toti08 Aug 09 '18 at 13:54

1 Answers1

0

The quickest way to fix this is to move your FirstName variable to the MegaQuiz as a class attribute and then use it from there. We can change your NEXT button command to call a class method and then from that method update the FirstName variable and then also set the FirstNameDis label text from that same method.

First move the FirstName variable into your MegaQuiz class by putting this line in the __init__ section.

self.FirstName = 'none'

Then change this:

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

To this:

    #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")

In your MainPage change this:

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

To this:

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

That should be all you need to fix this.

I did notice a few PEP8 issues so here is your code rewritten to provide a fix to your problem and also to rewrite some things to better follow the PEP8 guidelines.

  1. Not sure why you are adding () to some of your string variables but it is not needed.
  2. variable and class attribute names should be all lower case with underscores to between words.
  3. You do not need to provide a variable name for everything. If you have a button or a label you know you will not be updating later then you can write them without the variable names.

Code:

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()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79