【问题标题】:User input from GUI in Python 3.5Python 3.5 中来自 GUI 的用户输入
【发布时间】:2016-04-21 03:34:48
【问题描述】:

所以,我对 Python 还是很陌生,但是一旦将变量放入一个类中,我就无法处理它们。

当没有周围的类时,以下代码可以正常工作,但是一旦添加它就会出现错误:

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

任何帮助将不胜感激。

【问题讨论】:

  • GUI #calling the class to run 不。这不是你所说的。请先查看python类教程。
  • 这里有很多问题。您可能应该重新阅读TKinter 教程。
  • 另外,你应该在你的问题中描述你期望你的代码做什么。

标签: python user-interface tkinter python-3.5


【解决方案1】:

您的代码中有一些错误,并且没有以应有的方式使用类。我已经修改了您的代码并注释了新行,以便您了解正在发生的事情。我添加了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()

【讨论】:

    【解决方案2】:

    您的GUI 类格式不正确。您不应该只是将代码转储到一个类中,它应该在一个方法(属于一个类的函数)中。对于这样的事情,通常的做法是将其放入 __init__ 方法中,当您通过调用它来创建类的实例时会自动调用该方法。

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

    无论如何,我已经修复了您的代码,以便它运行;大概它会按照您的意图进行。请注意,这些方法的第一个参数是 self。请阅读一些关于类的 Python 文档/教程以获取更多信息。

    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)
    

    等等

    【讨论】:

    • 谢谢。我知道它远没有像样的 Python 代码,而且我研究过的教程没有像你上面那样进入类和方法设置。我想我必须尽快阅读一些好的教程。
    • @coelicasi:没问题。 import * 模式在示例代码(甚至官方文档/教程)中很常见,因为它使代码不那么冗长,但不幸的是,它让人们认为这是一个很好的实践。您可以在不将代码组织到类中的情况下进行简单的 GUI 操作,但是当您尝试构建复杂的 GUI 时,它很快就会变得混乱。除了查看教程之外,这里还有很多很棒的示例代码,尽管有时甚至专家也会使用import * 模式来保持简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2020-08-16
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多