Merhaba arkadaşlar;
Ben bir proje yapmaya çalışıyorum. Ama ne zaman butona bassam bu hatayı alıyorum, sizce sebebi nedir ? Yardımcı olursanız sevinirim. Tam olarak hata .:
Traceback (most recent call last):
File "C:\Users\Casper\Desktop\macro.py", line 26, in attack_macro
if a != state_left or b != state_right:
UnboundLocalError: local variable 'state_left' referenced before assignment
Hatayı tekrar edecek bir kod paylaşmamışsınız maalesef ama büyük ihtimalle state_left değişkenini 26.satırdan sonra bir yerde eşitliğin sol tarafında olacak şekilde kullanıyorsunuz, yani state_left = ... gibi. Bunu gören Python state_left’i yerel bir değişken olarak kaydediyor. Sonrasında fonksiyon çağrıldığında 26.satır öncesi state_left’ten eser olmamasına rağmen değerine erişmek istiyorsunuz, hata veriyor.
Şu da aynı tip hatayı verir:
def f():
if x > 3:
print(4)
x = 71
f()
UnboundLocalError: local variable 'x' referenced before assignment hatası alınıyor. Hatayı düzeltmek için state_left’i önceden bir yerlerde tanımlamanız gerekebilir, kodu tam bilemiyorum.
Bu arada bu hata NameError’den farklıdır, farkı da
state_left değişkenini 26.satırdan sonra bir yerde eşitliğin sol tarafında olacak şekilde kullanıyorsunuz, yani state_left = ... gibi.
Öyle ki şu kod
def g():
if y > 9:
print(2)
g()
NameError: name 'y' is not defined hatası veriyor.
@Then_Shiffman
Tam da o değişkeni kullanmıştım hemen değiştireyim ismini. Ama hala çalışmıyor hata vermiyor ama gerekeni de yapmıyor. kod şu
from PyQt5 import QtCore, QtGui, QtWidgets
import pyautogui
from pynput import keyboard
import time
import win32api
def place_macro():
while True:
left = win32api.GetKeyState(0x01)
right = win32api.GetKeyState(0x02)
a = win32api.GetKeyState(0x01)
b = win32api.GetKeyState(0x02)
if a != left or b != right:
left, right = a, b
if a < 0 or b < 0:
print('Hello World!')
pyautogui.click(button='right', clicks=100, interval=0.25)
def attack_macro():
while True:
left = win32api.GetKeyState(0x01)
right = win32api.GetKeyState(0x02)
a = win32api.GetKeyState(0x01)
b = win32api.GetKeyState(0x02)
if a != left or b != right:
left, right = a, b
if a < 0 or b < 0:
print('Hello World!')
pyautogui.click(button='left', clicks=100, interval=0.25)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(775, 590)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(110, 10, 531, 61))
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(80, 160, 231, 251))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(attack_macro)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(470, 160, 231, 251))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(place_macro)
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(30, 420, 731, 41))
font = QtGui.QFont()
font.setPointSize(13)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.clicktime = QtWidgets.QTextEdit(Form)
self.clicktime.setGeometry(QtCore.QRect(340, 250, 104, 71))
self.clicktime.setObjectName("clicktime")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(320, 190, 151, 21))
font = QtGui.QFont()
font.setPointSize(9)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(320, 220, 141, 16))
font = QtGui.QFont()
font.setPointSize(9)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", " you are using StaticSploit\'s program"))
self.pushButton.setText(_translate("Form", "Attack macro"))
self.pushButton_2.setText(_translate("Form", "place block macro"))
self.label_2.setText(_translate("Form", "for make attack macro on/off press alt gr. For make place macro on/off press right shift."))
self.label_3.setText(_translate("Form", "when you click how many "))
self.label_4.setText(_translate("Form", "times do you want"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Ne yapması gerektiğini veya win32api modülünün ayrıntılarını bilmiyorum maalesef, bir de konudan bağımsız olduğu için belki başka bir konu açmanız daha iyi olabilir veya başkaları buradan da yardımcı olabilirler belki, bilemiyorum.