0

I am making this Rock Paper Scissors game based on just image buttons to resemble rock paper scissors.

But now i want to assign a button to be "Rock" from my list. How would i do that? code:

    #Options
def Game():
    choice = ["Rock", "Paper", "Scissors"]
    RockButton = choice[0]
    PaperButton = choice[1]
    ScissorsButton = choice[2]

    #Naming the images
RockButton = Button(root, image = rock, bg="white", bd=0,command=Game).pack(side = LEFT)
PaperButton = Button(root, image = paper, bg="white", bd=0,command=Game).pack(side = RIGHT)
ScissorsButton = Button(root, image = scissors, bg="white", bd=0, command=Game).pack(pady=10)
trapgrave
  • 19
  • 2

1 Answers1

0

You can simplify things by using lambda functions for the command parameter of each button instead of using Game directly.

def Game(player_choice_string):
    print("player chose: {}".format(player_choice_string))

(RockButton := Button(root, image = rock, bg="white", bd=0,command=lambda:Game("rock"))).pack(side = LEFT)
(PaperButton := Button(root, image = paper, bg="white", bd=0,command=lambda:Game("paper"))).pack(side = RIGHT)
(ScissorsButton := Button(root, image = scissors, bg="white", bd=0, command=lambda:Game("scissors"))).pack(pady=10)

The walrus operator (:=) works in python >= 3.8 For older versions, you can just split the Button() and .pack() call onto two lines as shown here: Tkinter: AttributeError: NoneType object has no attribute <attribute name>

Michael Sohnen
  • 953
  • 7
  • 15
  • 1
    I don't think the `.format` is going to work properly without the `{}`. Also can you please fix [this](https://stackoverflow.com/q/1101750/11106801) before it becomes a problem. – TheLizzard Sep 11 '22 at 21:51
  • A question, how would i build a computer enemy w this – trapgrave Sep 12 '22 at 17:30
  • @trapgrave Stack overflow is not meant for very general questions such as "how would I build a computer enemy with this". You will have to try some options on your own, and come back with any specific/technical questions. Either way, if you like the answer, can you make sure to upvote and accept the answer (check mark). – Michael Sohnen Sep 12 '22 at 20:41