【问题标题】:Update User interface when a button is pressed Tkinter按下按钮时更新用户界面 Tkinter
【发布时间】:2014-03-04 16:25:09
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案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))
            ...
    

    【讨论】:

    • 这很有魅力,感谢您的快速响应
    【解决方案2】:

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

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

    root 然后将在全局模块命名空间中定义,函数中的代码可以在其中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多