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")
martineau
  • 119,623
  • 25
  • 170
  • 301
HaydenBrah
  • 13
  • 3
  • Please post your code as text. Code as a screenshot is never a good idea! – csabinho Nov 21 '19 at 00:26
  • always put code, data and full error message as text in question. – furas Nov 21 '19 at 00: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` – furas Nov 21 '19 at 00:39
  • Suggest you read accepted answer to [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time) since that sounds similar to what you want to do. – martineau Nov 21 '19 at 00:41

1 Answers1

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
furas
  • 134,197
  • 12
  • 106
  • 148