0

I have a tkinter button set up for sending a string to an empty list for comparison between a random computer choice of another string. Everything works accordingly, except that the following code for what to do with previously empty list executes before I am able to click the tkinter button. I know this is a weird way of programming a Rock, Paper, Scissors game. What's happening is when the "rock" button is clicked, the string "rock" is sent to an empty list for storing. The computer then picks one of three strings rock, paper, and scissors. That string is put into the other empty list for storing and immediately after to be compared with the first stored string from tkinter button. Which will give me the ability to determine who won or if it's a draw.

# Buttons for user to choose (Rock,Paper,Scissors).
button = Button(root, text="Rock", command=lambda: button_press.append("Rock"))
button.pack()

button_2 = Button(root, text="Paper", command=lambda: print("Paper"))
button_2.pack()

button_3 = Button(root, text="Scissors", command=lambda: print("Scissors"))
button_3.pack()

# Empty list for button value to be placed and compared with computers random.choice
button_press = []
comp_press = []

# Computer picks between the three options (Rock, Paper, Scissors)
rps = ["Rock"]

comp_pick = random.choice(rps)
comp_press.append(comp_pick)

if button_press == comp_press:
    print("Draw!")
else:
    print("Nothing")
4
  • Please post your code as text. Code as a screenshot is never a good idea! Commented Nov 21, 2019 at 0:26
  • always put code, data and full error message as text in question. Commented Nov 21, 2019 at 0:34
  • your problem is that you don't assign function to button. You assign some useless lambda to buttons and you try to run some code after you define buttons. You have command= for this - assign function which run your code after you click button. command=some_function_which_runs_code_after_pressing_button Commented Nov 21, 2019 at 0:39
  • Suggest you read accepted answer to Tkinter — executing functions over time since that sounds similar to what you want to do. Commented Nov 21, 2019 at 0:41

1 Answer 1

1

Button in GUI doesn't wait till you press it. It only defines what widget should be displayed by mainloop() which displays window. All code after Button is executed at once - even before you see window.

Your problem is that you assign useless lambda to buttons but you should assign function which check computer's selection with user's selection.

import tkinter as tk
import random

# --- functions ---

def check(user):
    computer = random.choice(rps)
    print(user, computer)

    if user == computer:
        print("Draw!")
    else:
        print("Nothing")

# --- main ---

rps = ["Rock", "Paper", "Scissors"]

root = tk.Tk()

button = tk.Button(root, text="Rock", command=lambda:check("Rock"))
button.pack()

button_2 = tk.Button(root, text="Paper", command=lambda:check("Paper"))
button_2.pack()

button_3 = tk.Button(root, text="Scissors", command=lambda:check("Scissors"))
button_3.pack()

root.mainloop() # start program
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.