So I'm writing code for a pyqt5 game to introduce me to the module and the button on the main screen doesn't seem to want to connect to the class method just below it. The code works fine when I copy paste it as an inner function inside the init method but doesn't connect at all when its connected to a method outside it. I've slimmed down the code to the part I'm focusing on
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
from threading import Thread
inventory = {"gold":0,"wood":0}
global window
class Screen(QMainWindow):
def start(self):
super(Screen,self).__init__()
self.setGeometry(300,300,600,600)
self.setWindowTitle("Game")
self.setStyleSheet("background: #161219;")
self.widgets = {"play": Button(0,"Play",(200,200)).butt}
self.show()
def clearscreen(self):
for key, value in self.widgets.items():
value.hide()
self.widgets = {}
app = QApplication(sys.argv)
window = Screen()
class Button():
def __init__(self, cooldown, text,pos):
self.butt = QPushButton(text,window)
self.text = text
self.butt.move(*pos)
self.cooldown = self.orgcooldown= cooldown
self.cooldownstate = True
self.butt.setFixedSize(200,100)
self.butt.setStyleSheet(
#setting variable margins
'''
*{border: 4px solid '#BC006C';
color: white;
font-family: 'shanti';
font-size: 15px;
border-radius: 40px;
padding: 15px 0;
margin-top: 20px}
*:hover{
background: '#BC006C'
}
'''
)
self.butt.setCursor(QCursor(Qt.PointingHandCursor))
self.butt.clicked.connect(self.clicked)
def clicked(self):
if self.cooldownstate:
match self.text:
case "Play":
window.clearscreen()
if __name__ == "__main__":
window.start()
sys.exit(app.exec_())