NameError:未定义全局名称“myText”

时间:2023-02-13 20:26:59

So I'm trying to build an insult generator that will take lists, randomize the inputs, and show the randomized code at the push of a button.

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

Right now, the code looks like...

现在,代码看起来像......

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

Right now, the error it's giving is that the myText isn't defined. Any thoughts on how to fix it?

现在,它给出的错误是没有定义myText。有关如何解决它的任何想法?

Edit: The error message is...

编辑:错误消息是...

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

3 个解决方案

#1


2  

If im dissectiong your code properly, you need to set def generator(): outside of the class you've defined; aka, make it a local function, not a method of myapp. secondly, you are trying to use the myText variable inside your onButtonClick method, but as your error states, it is not defined. in order to use the data your generator function is sending, you need to call the function. Just treat the last line of your def generator as a placeholder.

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

your accessor method should look like this:

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

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

(the entry widget is not needed, and you have not called .pack() on it yet ;) )

(不需要输入小部件,你还没有调用.pack();))

and your generator function should be outside of your class.

你的发电机功能应该在你的班级之外。

Alternatively, you could leave the generator function inside your class as a method, but you would need to add the self. arguement as a parameter to it:

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

def generator(self):

and add self.generator() when you call it in your button click event method.

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

Cheers!

#2


0  

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

AND

def generator(self):....

#3


-2  

change with OnButtonClick function's second line and replace myText with generator()

使用OnButtonClick函数的第二行更改并使用generator()替换myText

#1


2  

If im dissectiong your code properly, you need to set def generator(): outside of the class you've defined; aka, make it a local function, not a method of myapp. secondly, you are trying to use the myText variable inside your onButtonClick method, but as your error states, it is not defined. in order to use the data your generator function is sending, you need to call the function. Just treat the last line of your def generator as a placeholder.

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

your accessor method should look like this:

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

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

(the entry widget is not needed, and you have not called .pack() on it yet ;) )

(不需要输入小部件,你还没有调用.pack();))

and your generator function should be outside of your class.

你的发电机功能应该在你的班级之外。

Alternatively, you could leave the generator function inside your class as a method, but you would need to add the self. arguement as a parameter to it:

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

def generator(self):

and add self.generator() when you call it in your button click event method.

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

Cheers!

#2


0  

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

AND

def generator(self):....

#3


-2  

change with OnButtonClick function's second line and replace myText with generator()

使用OnButtonClick函数的第二行更改并使用generator()替换myText