How to pass input value to input() using pyqt5?

假装没事ソ 提交于 2020-06-09 05:38:06

问题


I created a poker game where the main function will ask the player(user) to type in an action (call/fold etc) in the console. The main function will call player's action() function (code below), and the function has two lines of input() to ask user to type in action type and bet size:

The player's action() function will keep be called whenever it's his turn, but the whole game will be on pause waiting for a value been passed to input() below.

   def action(self, chipsToCall : int, thisGameActions : dict):
        """
        Position based strategy, Fixed range
        """

        nChip = self._getCurrentStack(thisGameActions)
        community_card = []
        community_card = thisGameActions['CommunityCards']['Flop'] + thisGameActions['CommunityCards']['Turn'] + thisGameActions['CommunityCards']['River']

        print("\n***************************")
        print('current street ', thisGameActions['Street'], ' your position ', self.position)
        print('your hand ', str(self.hand[0]), str(self.hand[1]))
        print('your stack ', str(nChip))
        print('bet to call ', str(chipsToCall))
        print('current pot', str())
        print('community cards ', thisGameActions['Pot'])
        for i in community_card:
            #print(i.prettyCard())
            print(i)

        print('current Street Bet History')
        print(thisGameActions['BetHistory'])

        action = input("type in your action: ")
        bet = int(input("type in your bet (int): "))
        print("\n***************************")
        return action, bet

My question is, how can i create a pyqt5 widget to pass value from a buttom (for example) to the input() function above?

Right now, my gui code will run the game by calling the main function, but it will still ask for input in the console.


update:

I realize my language might be confusing , below is my full source code for this poker game I wrote.

https://github.com/TerryDuan/PokerGame

The test.py shows an example of how the game will be execute, when one of the player is the interactivePlayer, the console will ask user to type in action and bet size in every game (and on each 'street').

For example:

import os
import sys
sys.path.append("..")
from deckClass import Deck
from playerClass import Player
from smarterPlayer import smarterPlayer
from interactivePlayer import interactivePlayer
from PokerTableClass import table

if __name__ == "__main__":
    print("TEST TEST TEST")

    p1 = Player("p1")
    p2 = Player("p2")
    p3 = Player("p3")
    p4 = Player("p4")
    p5 = interactivePlayer("ip5")
    p6 = smarterPlayer("sp6")

    aTable = table()
    aTable.setMaxGames(1000)

    aTable.addPlayer(p1,100)
    aTable.addPlayer(p2,100)
    aTable.addPlayer(p3,100)
    aTable.addPlayer(p4,100)
    aTable.addPlayer(p5,100)
    aTable.addPlayer(p6,100)

    print("Table Setted up with Six dummy player")
    print("Number of Players " + str(aTable.getNPlayer()))
    print("Number of Active Players " + str(aTable.getNPlayer_active()))
    print("Number of inGame Players " + str(aTable.getNPlayer_inGame()))

    aTable.runGame()

Above code will kick off a complete poker game (1000 iteration max), and user can only 'play' with it on python console.

My goal is to build a gui on it, to replace the python console. I was trying to find if there is a way to overwrite input() function, so that it will wait for the value from the UI window. But so far I didn't find any article/post that can help me.


update 2

as a simplified example: originally, I have below code to keep asking user to type in a command

class player():
   ...
   def action(self):
      action = input("please type in a command: ")
      return action

p1 = player()
while True:
   action = p1.action()
   if action == 'exit':
      break #end of the game

Here user can only 'play' with it on python console How can I use pyqt5 to build a UI with buttoms (corresponding to diff type of actions) to run this game, instead of on a python console


回答1:


You need to create a widget (let's say window) containg either, one button per action, or an input field where to type your action. If you consider the second choice it would be something like:

from PyQt5.Widgets import QLabel, QDialog, QPushButton

class InputWindow(QDialog):

    pokerActions = ["Action_1",.....,"Action_n"]

    def __init__(self, parent=None)
    super().__init__()
    self.setUI()

    def setUI(self):
        lblAction = QLabel("Type your Action")
        self.lineAction = QLineEdit()
        self.lineAction.editingFinished.connect(self.sendAction)

        layout = QHBoxLayout()
        layout.addWidget(lblAction)
        layout.addWidget(self.lineAction)
        self.setLayout(layout)


    @pyqtSlot()
    def sendAction(self):
        if self.lineAction.text() not in pockeActions:
            return
        parent.getAction(self.lineAction.text()

You will need to open this widget from the calling class, probably when each player do his call as:

    act = InputWindow(self)
    act.show()
    act.exec()

And you may want to replace your action method by:

   def getAction(action):
       return action 


来源:https://stackoverflow.com/questions/62185599/how-to-pass-input-value-to-input-using-pyqt5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!