[英]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
我在function 中创建了一个变量,但它说“全局变量在‘模块级别’未定义” 。 也许我的变量是不同的类型,但我不知道如何修复它。 在我在 function 之外使用它之后,它说变量未定义。
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)
如果您想知道,我正在使用 tkinter 和其他一些库。 另外,这是我所有的代码:
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()
所以如果可以的话请帮忙
关键字“global”不定义新变量。 它只是将变量从全局 scope 拉到本地。 因此,由于全局scope中没有selection变量,所以不起作用。 您是否考虑过使用 return 关键字从 function 返回值,而不是使用和修改全局变量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.