2014-01-07 4 views
0

PyQt5에서 필자가 사용하는 함수 중 몇 개를 호출한다고해도 지금까지 여러 번 호출했습니다. 나는 관련 코드에 물을 주려고 노력할 것이다. 함수 호출 및 여러 번 호출하기

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) 

지금 내가 i.buy_ship 호출 할 때마다 그것을 내가 지금까지 (이 호출 처음으로, 두 번째는 내가 두 번 호출 버튼 등을 눌러)라고했습니다 그러나 여러 번 호출합니다. 나는 그것이 놀이에 있어야하는 것처럼 느낀다. 그러나 나는 나 자신의 인생이 그것을 발견 할 수 없다.

편집는 플레이어 클래스의 기능을 buy_ship 추가

+0

우리는'buy_ship' 기능을 볼 수 있습니까? 나는 그것이 그럴 가능성이 더 크다고 생각합니다. –

+0

buy_ship 함수 이상으로 발생하지만 확실합니다. – Faller

답변

1

각 시간 buy_ship가 호출 버튼에 기능을 결합하고 있기 때문에 그것은있을 수 있습니다. 두 번째로 buy_ship을 호출합니다. 이전 바인딩이 아직 있습니다.

window.stackedWidget.setCurrentIndex(4) 
window.btn_buy.clicked.connect(purchase) 
window.btn_back_to_SY.clicked.connect(cancel_purchase) 
+0

그래, 이것이 문제 야. 연결을 한 번만 수행하거나 적절한 키워드를 사용하여 단일 연결을 보장하십시오. – amicitas

+0

나는 그것을 시도 할 것이다, 당신은 "적절한 키워드"를 의미합니까 – Faller

+0

아름답게 작동합니다. M4rtini 및 amicitas를 통해 인터넷에 감사드립니다. – Faller