2014-03-04 42 views
0

所以基本上我希望用户界面在用户按下开始按钮时更新,但是当我调用maininit函数时它会返回一个错误,指出root没有定义,是任何方式我可以解决这个问题?按下按钮时更新用户界面Tkinter

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

回答

1

在您的应用程序,root是局限于main()本地定义的变量。你需要以某种方式通过root作为参数maininit。下面是做到这一点的一种方法:

首先,更改maininit()使其接受一个参数root

def maininit(root): 
    ... 

现在,更改StartButton回调所以它传递maininit()root对象:

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

这工作的魅力,感谢您的快速反应 – Dolan

0

您正在定义root函数,其函数命名空间不可用于maininit()函数。如果省略的main()的定义,而是写

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

root随后将在全球模块命名空间,它是提供给函数的代码来定义。