Question

So basically i want the user interface to be updated when a user presses the start button, however when i call the maininit function it returns an error saying that root is not defined, is there any way i can get around this?

from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH,W, N, E, S, Entry, Text, INSERT, Toplevel
from ttk import Frame, Style, Button, Label
import Tkinter
import Callingwordlist

class MainGameUI(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   

        self.parent = parent

        self.initUI()

    def initUI(self):

        self.parent.title("Type!")
        self.pack(fill=BOTH, expand=1)

        style = Style()
        style.configure("TFrame", background="black")

        Type = Image.open("Type!.png")
        Typet = ImageTk.PhotoImage(Type)
        label3 = Label(self, image=Typet)
        label3.image = Typet
        label3.place(x=0, y=0)


        self.pack(fill=BOTH, expand=1)

        MenuButton = Button(self, text="Main Menu")
        MenuButton.pack()
        MenuButton.place(x=560,y=20,height = 80,width = 100)

        QuitButton = Button(self,text="Quit",command=self.parent.destroy)
        QuitButton.pack()
        QuitButton.place(x=680,y=20,height = 80,width = 100)

        StartButton = Button(self, text="Start",command=maininit)
        StartButton.pack()
        StartButton.place(x=440,y=20,height=80,width=100)

def maininit():

    entry1 = Entry(root,font =("Courier",38), width = 22)
    entry1.pack(ipady=10)
    entry1.config(bg="#CEF6F5")
    entry1.place(x=90,y=200)

    entry2 = Entry(root,font =("Courier",38), width = 22)
    entry2.pack(ipady=10)
    entry2.config(bg="#CEF6F5")
    entry2.place(x=90,y=350)

    text1 = Text(root,width=23,height=1,font=("Courier",38))
    text1.pack()
    text1.config(bg="black",fg="white",bd=0)
    text1.place(x=90,y=150)
    text1.insert(INSERT,"Hello")

    text2 = Text(root,width=23,height=1,font=("Courier",38))
    text2.pack()
    text2.config(bg="black",fg="white",bd=0)
    text2.place(x=90,y=300)
    text2.insert(INSERT,"Test")

    dtext = Text(root,font=("Courier",28),width=10,height=1)
    dtext.pack()
    dtext.config(bg="black",fg="white",bd=0)
    dtext.insert(INSERT,"Difficulty")
    dtext.place(x=90,y=500)

    atext = Text(root,font=("Courier",28),width=8,height=1)
    atext.pack()
    atext.config(bg="black",fg="white",bd=0)
    atext.insert(INSERT,"Accuracy")
    atext.place(x=595,y=500)

    dentry = Text(root,font=("Courier",28),width=1,height=1)
    dentry.pack()
    dentry.config(bg="white",bd=0)
    dentry.place(x=180,y=550)
    dentry.insert(INSERT,"Test")

def main():

    root = Tk()
    root.geometry("860x640+300+300")
    app = MainGameUI(root) 
    root.mainloop()

if __name__ == '__main__':
    main()
Was it helpful?

Solution

In your application, root is a locally defined variable confined to main(). You need to somehow pass root as an argument to maininit. Here is one way to do this:

First, change maininit() so that it accepts a parameter root:

def maininit(root):
    ...

Now, change the callback on StartButton so it passes maininit() the root object:

class MainGameUI(Frame):
    ...
    def initUI(self):
        ...
        StartButton = Button(self, text="Start",command=lambda: maininit(self.parent))
        ...

OTHER TIPS

You are defining root inside a function, whose calling namespace is not available to the maininit() function. If you omit the definition of main() and instead write

if __name__ == "__main__":
    root = Tk()
    root.geometry("860x640+300+300")
    app = MainGameUI(root) 
    root.mainloop()

root will then be defined in the global module namespace, where it IS available to the code in the function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top