Python 3.5 中来自 GUI 的用户输入

User input from GUI in Python 3.5

所以,我是 Python 的新手,但是一旦将变量放在 class 中,我就无法处理变量。

当周围没有 class 时,以下代码可以正常工作,但是一旦我添加它,我就会收到错误消息:

NameError: name 'someName' is not defined

发生在第 3 行

text = "You have entered " + someName.get()

代码如下:

class GUI:
    def changeLabel():
        text = "You have entered " + someName.get()
        labelText.set(text)
        someName.delete(0, END)
        someName.insert(0, "You've clicked!")
        return

    app = Tk()
    app.title("GUI Test")
    app.geometry('450x300')

    labelText = StringVar()
    labelText.set("Click when ready")
    label1 = Label(app, textvariable=labelText, height=4)
    label1.pack()

    userInput = StringVar(None)
    someName = Entry(app, textvariable=userInput)
    someName.pack()

    button1 = Button(app, text="Click Here", width=20,command=changeLabel)
    button1.pack(side='bottom',padx=15,pady=15)

    app.mainloop()

GUI #calling the class to run

如有任何帮助,我们将不胜感激。

您的代码中存在一些错误,并且未按应有的方式使用 类。我已经修改了您的代码并对新行进行了注释,以便您了解发生了什么。我添加了 self 对所有文本变量的引用,以便可以正确访问它们。

from tkinter import *

class GUI:
        #set these so that they are able to be used by the whole class
        labelText = ""
        userInput = "" 

        #you should have an init method for your classes and do all the setup here
        def __init__(self,master):   
                self.labelText = StringVar()
                self.userInput = StringVar()

                #you should pack things into a frame
                frame = Frame(master)     
                frame.pack()

                self.labelText.set("Click when ready")
                label1 = Label(frame, textvariable=self.labelText, height=4)
                label1.pack()

                someName = Entry(frame, textvariable=self.userInput)
                someName.pack()

                button1 = Button(frame, text="Click Here", width=20,command=self.changeLabel)
                button1.pack(side='bottom',padx=15,pady=15)

        def changeLabel(self):
                text = "You have entered " + self.userInput.get()
                self.labelText.set(text)
                self.userInput.set("You've clicked!")
                return

#create the app before you call the GUI.
app = Tk()
app.title("GUI Test")
app.geometry('450x300')

# when you create the class, you need to assign it to a variable
applet = GUI(app) #calling the class to run
app.mainloop()

你的 GUI class 不是 well-formed。您不应该只是将代码转储到 class 中,它应该在方法中(属于 class 的函数)。对于这样的事情,通常的做法是将它放在 __init__ 方法中,当您通过调用它创建 class 的实例时,该方法会自动调用。

方法可以有自己的局部变量,也可以使用self.attribute_name语法访问实例的属性。您的 NameError: name 'someName' is not defined 错误消息是因为 Python 认为 someNamechangeLabel 的局部变量,它没有意识到它应该是您的 Entry 小部件.

无论如何,我已经修复了您的代码以便它运行;大概它做了你想要的。请注意,这些方法将 self 作为其第一个参数。请阅读一些 Python 有关 class 的文档/教程以获取更多信息。

from Tkinter import *

class GUI(object):
    def changeLabel(self):
        text = "You have entered " + self.someName.get()
        self.labelText.set(text)
        self.someName.delete(0, END)
        self.someName.insert(0, "You've clicked!")            

    def __init__(self):
        app = Tk()
        app.title("GUI Test")
        app.geometry('450x300')

        self.labelText = StringVar()
        self.labelText.set("Click when ready")
        label1 = Label(app, textvariable=self.labelText, height=4)
        label1.pack()

        userInput = StringVar(None)
        self.someName = Entry(app, textvariable=userInput)
        self.someName.pack()

        button1 = Button(app, text="Click Here", width=20,command=self.changeLabel)
        button1.pack(side='bottom',padx=15,pady=15)

        app.mainloop()

GUI() #calling the class to run

此代码适用于 Python 2;您需要将 Python 3 上的 import 语句更改为 from tkinter import *。实际上,不建议使用 import *,因为它会用所有导入的名称污染您的命名空间。最好做类似

的事情
import tkinter as tk

然后像这样写你的 Tkinter 参考文献

app = tk.Tk()

label1 = tk.Label(app, textvariable=self.labelText, height=4)

等等