简体   繁体   中英

I declare a global variable inside a function, but outside the function it is "undefined", it also says inside the function that it is undefined

I made a variable inside the function, but it says " Global variable is undefined at the 'module level' " . Maybe my variable is a different type, but I don't know how to fix it. After I use it outside the function it it says that the variable in undefined.

def selectSpeed():
    global selection
    selection = (str(speed.get()), "ms")
    label3.config(text=selection)

...

label4 = Label(root, text=selection + "Hi")
label4.place(x=15, y=260)

I am using tkinter and a few other libraries in case you want to know. Also, this is all the code I have:

import pyautogui
import webbrowser
import time
import threading
import copy
import tkinter as tk
from tkinter import *
import tkinter.font as fnt
import random


def attack():
    normal = input("Do you want the program to be slower (s) fast (f) or extra fast BETA (b)?   ")
    repeats = int(input("How many times do you want to send this message?  "))
    delay = int(input("how many ms do you want to wait in between each message?   "))

    isLoaded = input("Press enter when your app has loaded")

    print("You have 5 seconds until the spam begins")
    real_repeats = repeats
    if normal == "f":
        real_repeats = int(repeats / 33)
    if normal == "b":
        real_repeats = int(repeats / 87)

    time.sleep(5)

    def spam():
        for i in range(0, real_repeats):
            pyautogui.hotkey('ctrl', 'v')
            pyautogui.press("enter")
        time.sleep(delay / 1000)

    def super_spam():
        thread1 = threading.Thread(target=spam)
        thread1.start()
        thread2 = threading.Thread(target=spam)
        thread2.start()
        thread3 = threading.Thread(target=spam)
        thread3.start()
        thread4 = threading.Thread(target=spam)
        thread4.start()
        thread5 = threading.Thread(target=spam)
        thread5.start()
        thread6 = threading.Thread(target=spam)
        thread6.start()
        thread7 = threading.Thread(target=spam)
        thread7.start()
        thread8 = threading.Thread(target=spam)
        thread8.start()
        thread9 = threading.Thread(target=spam)
        thread9.start()
        thread10 = threading.Thread(target=spam)
        thread10.start()
        thread11 = threading.Thread(target=spam)
        thread11.start()
        thread12 = threading.Thread(target=spam)
        thread12.start()
        thread13 = threading.Thread(target=spam)
        thread13.start()
        thread14 = threading.Thread(target=spam)
        thread14.start()
        thread15 = threading.Thread(target=spam)
        thread15.start()
        thread16 = threading.Thread(target=spam)
        thread16.start()
        thread17 = threading.Thread(target=spam)
        thread17.start()
        thread18 = threading.Thread(target=spam)
        thread18.start()
        thread19 = threading.Thread(target=spam)
        thread19.start()
        thread20 = threading.Thread(target=spam)
        thread20.start()
        thread21 = threading.Thread(target=spam)
        thread21.start()
        thread22 = threading.Thread(target=spam)
        thread22.start()
        thread23 = threading.Thread(target=spam)
        thread23.start()
        thread24 = threading.Thread(target=spam)
        thread24.start()
        thread25 = threading.Thread(target=spam)
        thread25.start()
        thread26 = threading.Thread(target=spam)
        thread26.start()
        thread27 = threading.Thread(target=spam)
        thread27.start()
        thread28 = threading.Thread(target=spam)
        thread28.start()
        thread29 = threading.Thread(target=spam)
        thread29.start()
        thread30 = threading.Thread(target=spam)
        thread30.start()
        thread31 = threading.Thread(target=spam)
        thread31.start()
        thread32 = threading.Thread(target=spam)
        thread32.start()

    def mega_spam():
        threadM1 = threading.Thread(target=super_spam)
        threadM1.start()
        threadM2 = threading.Thread(target=super_spam)
        threadM2.start()
        threadM3 = threading.Thread(target=super_spam)
        threadM3.start()

    if normal == "s":
        spam()

    if normal == "f":
        super_spam()

    if normal == "b":
        mega_spam()


root = Tk()
root.title('Spam Program')
root.geometry("550x280")

button = Button(root, text='Start Spamming', command=attack, font=fnt.Font(size=20), activebackground="light gray",
                bd=10)
button.place(x=205, y=15)


def selectMode():
    speed1 = str(mode.get())


mode = IntVar()
Radiobutton(root, text=' Slower', variable=mode, value=1, font=fnt.Font(size=15), command=selectMode).place(x=15, y=15)
Radiobutton(root, text=' Faster', variable=mode, value=2, font=fnt.Font(size=15), command=selectMode).place(x=15, y=65)
Radiobutton(root, text=' BETA', variable=mode, value=3, font=fnt.Font(size=15), command=selectMode).place(x=15, y=115)


def selectSpeed():
    global selection
    selection = (str(speed.get()), "ms")
    label3.config(text=selection)


speed = DoubleVar()
restspeed = Scale(root, from_=210, to=3000, orient=HORIZONTAL, length=320, bd=5, variable=speed, sliderlength=40)
restspeed.place(x=205, y=120)
button = Button(root, text="Set Spam Speed", command=selectSpeed, bd=5)
button.place(x=360, y=200)

label3 = Label(root)
label3.place(x=250, y=210)

times = Spinbox(root, from_=1, to=100001, width=6)
times.place(x=150, y=210)

label4 = Label(root, text=selection + "Hi")
label4.place(x=15, y=260)

mainloop()

So please help if you can

The keyword "global" does not define a new variable. It just pulls a variable from global scope to local. Therefore, as there is no variable called selection in the global scope, it does not work. Rather than using and modifying global variables, have you considered returning the value from the function using the return keyword?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM