按下按钮时更新用户界面Tkinter

时间:2014-03-04 16:25:10

标签: python function python-2.7 user-interface tkinter

所以基本上我想要在用户按下开始按钮时更新用户界面,但是当我调用maininit函数时它会返回一个错误,说明root没有定义,有什么方法可以解决这个问题吗? / p>

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

2 个答案:

答案 0 :(得分: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))
        ...

答案 1 :(得分:0)

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

if __name__ == "__main__":
    root = Tk()
    root.geometry("860x640+300+300")
    app = MainGameUI(root) 
    root.mainloop()
然后将在全局模块命名空间中定义

root,该命名空间可供函数中的代码使用。