1

I was trying to figure out how I can display "it's red's turn", than after red click's it will display it's blue's turn and vice versa.

This is what I tried doing so far:


    if redPlayer == True:
        RedTurn = Label(display,
                        text="It's Red Player's turn",
                        fg='red',
                        font="Times 32",
                        width=30,
                        height=2)
        RedTurn.pack()

    if bluePlayer == True:
        BlueTurn = Label(display,
                        text="It's Blue Player's turn",
                        fg='blue',
                        font="Times 32",
                        width=30,
                        height=2)
        BlueTurn.pack()
4
  • Why don't you create 1 label then use <tkinter.Label>.config(text=<new text>) Commented Apr 21, 2021 at 17:11
  • Where could I implement this in my code? Commented Apr 21, 2021 at 17:15
  • When you are setting up your window just add label = Label(display, ...) and label.pack() then in that function you can just call label.config(text=<new text>) Commented Apr 21, 2021 at 17:17
  • I can't give you a proper answer unless you show me the code that creates the display and how that function is called. Commented Apr 21, 2021 at 17:17

2 Answers 2

1

Change a label's text with config(text=progress):

from tkinter import *

def clicked():
    l.config(text='changed!')
    
    
root = Tk()

l = Label(text='Click button to change text')
b = Button(text='click', command=clicked)

l.pack()
b.pack()

mainloop()
Sign up to request clarification or add additional context in comments.

Comments

0

how I can display "it's red's turn", than after red click's it will display it's blue's turn and vice versa.

Create your_turn() function.

  • Just use one Label widget instead of a duplicated Label widget.
  • Add your_turn_label.configure in your_turn() function.

Snippet:

import tkinter as tk


redPlayer: bool = True
bluePlayer: bool = False

def your_turn():
    global redPlayer, bluePlayer
    if redPlayer:
        your_turn_label.configure(text="It's Red Player's turn")
        your_turn_label.configure(fg='red')
        redPlayer = not redPlayer
    else:                 
        your_turn_label.configure(text="It's Blue Player's turn")
        your_turn_label.configure(fg='blue')
        redPlayer = not redPlayer   
                    
           
root = tk.Tk()

your_turn_label = tk.Label(text='Click button to change text', font="Times 32", width=30,height=2, )
your_turn_button = tk.Button(text='click', command=your_turn)

your_turn_label.pack()
your_turn_button.pack()

root.mainloop()

Screenshot:

enter image description here

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.