0

I am trying to configure the button based on the value of turn which is determined by the while loop which is determined by the value of game_over

The while loop is interfering with the main loop of tkinter. I can't seem to figure out a way around this.

This is the code.

from tkinter import *
connect = Toplevel()

col1 = Button(connect, width=10, height=4, text=" ")
col1.grid(row=0, column=1)

game_over = False
turn = 0

while not game_over:
    if turn == 0:
        col1.configure(bg="#FE6869")
        x=input("Enter something")
        if x=="yes":
            game_over=True
        else:
            continue   
    else:       
        col1.configure(bg="#FFFC82")
        x=input("Enter something")
        if x=="no":
            game_over=True
        else:
            continue
    turn += 1
    turn = turn % 2

connect.mainloop()
1
  • It's running, but you're preventing mainloop from being able to update the display. Commented Oct 21, 2022 at 15:53

1 Answer 1

5

First, I would not recommend to use potentially infinite while loops. Second you should check how to create a tkinter application, first create your root window before you create any TopLevel widget. Next, buttons can execute functions specified as command. Lastly, if you work with a GUI based approach, you can get the user input as well from the GUI. I made an example application from your code. I highly recommend you to watch some tutorials on tkinter before continuing.

import tkinter as tk
import tkinter.simpledialog as sd


def start():
    global turn
    global game_over

    if not game_over:
        if turn == 0:
            col1.configure(bg="#FE6869")
            x=sd.askstring('User Input', 'Enter something')
            if x=="yes":
                game_over=True
                end = tk.Label(connect, text='GAME OVER')
                end.grid(row=1, column=0)
            else:
                pass
        else:
            col1.configure(bg="#FFFC82")
            x=sd.askstring('User Input', "Enter something")
            if x=="no":
                game_over=True
                end = tk.Label(connect, text='GAME OVER')
                end.grid(row=1, column=0)
            else:
                pass
        turn += 1
        turn = turn % 2


connect = tk.Tk()

turn = 0
game_over = False

col1 = tk.Button(connect, width=10, height=4, text=" ", command=start)
col1.grid(row=0, column=0)

connect.mainloop()
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.