Reputation: 1708
I'm having an issue in PyQt5 where if I some of my functions, it calls it however many times I've called it so far. I'll try to water down to relevant code.
class MainWindow(QtWidgets.QMainWindow, UI.MainUI.Ui_MainWindow):
"""The Main Window where everything happens"""
def __init__(self, parent=None):
"""Initializes (Don't use partial as most of the variables
aren't created yet)"""
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_buy_ship.clicked.connect(
lambda: self.game.current_player.buy_ship(self))
def new_game(self):
"""Runs tha actual Game"""
self.game = Game(self)
self.game.play(self)
class Game(object):
"""The Game Class"""
def __init__(self, window):
"""The Obvious"""
self.window = window
self.makeshitgo = True
super(Game, self).__init__()
def play(self, window):
"""starts the game"""
while self.makeshitgo:
for i in self.players:
self.current_player = i
if i.ship.name is None:
i.buy_ship(window)
while self.myturn:
QtWidgets.qApp.processEvents()
self.current_player.update_cargo(window)
time.sleep(.05)
self.turn += 1
class Player:
def __init__(self, ship, port, name):
"""Starts the player off with 0 everything and 5000 deblunes"""
self.name = name
def buy_ship(self, window):
"""Stops execution until ok/cancel is pressed"""
window.change_ship("NA", "Galleon")
def purchase():
"""buys the ship and updates money""" # needs sell old ship
if self.money >= int(window.V_Price.text()):
self.ship = Ship(window.H_Ship_Name.text())
self.change_money("down", self.ship.cost, window)
window.textBrowser.append(self.ship.name)
window.to_shipyard()
else:
window.textBrowser.append("You can't afford that brokearse")
def cancel_purchase():
"""If you don't want to make a purchase"""
if self.ship.name is None:
window.textBrowser.append("You need a ship")
else:
window.to_shipyard()
window.stackedWidget.setCurrentIndex(4)
window.btn_buy.clicked.connect(purchase)
window.btn_back_to_SY.clicked.connect(cancel_purchase)
Now every time I call i.buy_ship it calls it however many times I've called it so far(the first time it calls, the second time I push the button it calls twice, etc). I feel as if it has to be in play() but I can't for the life of me find it.
Edit added buy_ship function in Player class
Upvotes: 0
Views: 894
Reputation: 13549
It could be because you're binding a function to the buttons each time buy_ship is called. So the second time you call buy_ship. The previous binding is still there.
window.stackedWidget.setCurrentIndex(4)
window.btn_buy.clicked.connect(purchase)
window.btn_back_to_SY.clicked.connect(cancel_purchase)
Upvotes: 1