首页 > 解决方案 > 如何在每一轮中使用 PIL 添加带有 4 个图像的 GUI

问题描述

我想用 tkinter 和 PIL 模块添加骰子 6 面的图像,但我做不到。我该怎么做?共有 4 张图像,一个按钮可以用 4 张图像掷骰子。这就是我要的。感谢您的帮助。我在比赛的每一轮都需要它。这是一个 4 人的骰子游戏。

    import random
    
    def main():
        player1 = 0
        player1wins = 0
        player2 = 0
        player2wins = 0
        player3 = 0
        player3wins = 0
        player4 = 0
        player4wins = 0
        rounds = 1
    
        while rounds != 11:
            print("Round " + str(rounds))
            player1 = dice_roll()
            player2 = dice_roll()
            player3 = dice_roll()
            player4 = dice_roll()
            print("Player 1 Roll: " + str(player1))
            print("Player 2 Roll: " + str(player2))
            print("Player 3 Roll: " + str(player3))
            print("Player 4 Roll: " + str(player4))
    
            if((player1>player2 and player1>player3 and player1>player4) and (player1 != player2 and player1 != player3 and player1 != player4)):
                player1wins = player1wins + 1
                print("Player 1 wins!\n")
            elif((player2>player1 and player2>player3 and player2>player4) and (player2 != player1 and player2 != player3 and player2 != player3)):
                player2wins = player2wins + 1
                print("Player 2 wins!\n")
            elif((player3>player1 and player3>player2 and player3>player4) and (player3 != player1 and player3 != player2 and player3 != player4)):
                player3wins = player3wins + 1
                print("Player 3 wins!\n")
            elif((player4>player1 and player4>player2 and player4>player3) and (player4 != player1 and player4 != player2 and player4 != player3)):
                player4wins = player4wins + 1
                print("Player 4 wins!\n")
            else:
                print("Round Draw! \n")
    
            rounds = rounds + 1
    
        if((player1wins>player2wins and player1wins>player3wins and player1wins>player4wins) and (player1wins != player2wins and player1wins != player3wins and player1wins != player4wins)):
                print("Player 1 Wins The Match \n\nRounds Won: " + str(player1wins))
        elif((player2wins>player1wins and player2wins>player3wins and player2wins>player4wins) and (player2wins != player1wins and player2wins != player3wins and player2wins != player3wins)):
                print("Player 2 Wins \n\nRounds Won: " + str(player2wins))
        elif((player3wins>player1wins and player3wins>player2wins and player3wins>player4wins) and (player3wins != player1wins and player3wins != player2wins and player3wins != player4wins)):
                print("Player 3 Wins \n\nRounds Won: " + str(player3wins))
        elif((player4wins>player1wins and player4wins>player2wins and player4wins>player3wins) and (player4wins != player1wins and player4wins != player2wins and player4wins != player3wins)):
                print("Player 4 Wins \n\nRounds Won: " + str(player4wins))
        else:
                print("Match Draw!")
    
        def dice_roll():
        diceRoll = random.randint(1, 6)
        return diceRoll
    
        main()

标签: pythontkinterpython-imaging-library

解决方案


推荐阅读