[英]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.