Python 名称错误:全局名称';myText';没有定义

Python 名称错误:全局名称';myText';没有定义,python,user-interface,Python,User Interface,因此,我试图构建一个侮辱生成器,它将获取列表,随机输入,并在按下按钮时显示随机代码 现在,代码看起来像 import Tkinter import random section1 = ["list of stuff"] section2 = ["list of stuff"] section3 = ["list of stuff"] class myapp(Tkinter.Tk): def __init__(self,parent): Tkinter.Tk.__in

因此,我试图构建一个侮辱生成器,它将获取列表,随机输入,并在按下按钮时显示随机代码

现在,代码看起来像

import Tkinter
import random


section1 = ["list of stuff"]
section2 = ["list of stuff"]
section3 = ["list of stuff"]

class myapp(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid() # creates grid layout manager where we can place our widgets within the window

        button = Tkinter.Button(self, text=u"Generate!", command=self.OnButtonClick)
        button.grid(column=1, row=0)

        self.labelVariable = Tkinter.StringVar()

        label = Tkinter.Label(self, textvariable=self.labelVariable, anchor='w', fg='white', bg='green')
        label.grid(column=0, row=1, columnspan=2, sticky='EW')
        self.labelVariable.set(u"Oh hi there !")

        self.grid_columnconfigure(0, weight=1)
        self.resizable(True, False)
        self.update()
        self.geometry(self.geometry())

    def generator():
        a = random.randint(0, int(len(section1))-1)
        b = random.randint(0, int(len(section2))-1)
        c = random.randint(0, int(len(section3))-1)
        myText = "You are a "+ section1[a]+" "+section2[b]+'-'+section3[c]+"! Fucker."
        return myText

    def OnButtonClick(self):
        self.labelVariable.set(myText + "(You clicked the button !)") 
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)


if __name__=='__main__':
    app = myapp(None) # Instantiates the class
    app.title('Random Insult Generator') # Names the window we're creating.
    app.mainloop() # Program will loop indefinitely, awaiting input
现在,它给出的错误是没有定义
myText
。 有没有关于如何修复它的想法

编辑: 错误消息是

Exception in Tkinter callback
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "...", line 41, in OnButtonClick
    self.labelVariable.set(myText+"(You clicked the button !)")
NameError: global name 'myText' is not defined


用onbutton更改单击函数的第二行并用generator()替换myText

如果我正确解析代码,则需要将def generator()设置为您定义的类之外;aka,让它成为本地函数,而不是myapp的方法。其次,您试图在onButtonClick方法中使用myText变量,但正如错误所述,它没有定义。为了使用生成器函数正在发送的数据,需要调用该函数。只需将def生成器的最后一行视为占位符即可

您的访问器方法应如下所示:

def OnButtonClick(self):
    self.labelVariable.set(generator()+"(You clicked the button !)") 
    #self.entry.focus_set()
    #self.entry.selection_range(0,Tkinter.END)
(不需要entry小部件,而且您尚未对其调用.pack()))

你的生成器函数应该在你的类之外

或者,您可以将生成器函数作为方法留在类中,但需要添加self。作为参数的参数:

def generator(self):
并在按钮单击事件方法中调用self.generator()时添加它


干杯

错误消息有什么不清楚的地方?错误显然发生在
OnButtonClick
中,实际上,您还没有在该函数中定义
myText
。您是否有可能在某个时候将您的技能转向编写有用的软件?:-)@paxdiablo嗯,他可能需要侮辱一些人,但也许他没有时间考虑侮辱☺ (这实际上是一个微笑,但在这个字体大小下它看起来像一个皱眉)应该是小写的
generator()
,应该用
self调用。generator()
self
应该作为参数添加到generator中,就像
def generator(self)一样
+1的正确答案,加上将
生成器
功能移出课堂的建议。它现在真的不应该在类中,这将把名称错误改为“generator”而不是“myText”。
def OnButtonClick(self):
    self.labelVariable.set(generator()+"(You clicked the button !)") 
    #self.entry.focus_set()
    #self.entry.selection_range(0,Tkinter.END)
def generator(self):