mahiatlinux
mahiatlinux

Reputation: 163

How to change colour when I click a button in guizero Python 3?

So I have written this piece of code in python3 guizero to change colour to red when I click the button I created. But whatever I do it's not working! I am not sure what I did wrong but it just won't work (I used Visual Studio Code but it didn't give me any errors or say the code was wrong.) So I figured this will be the best place to come. This is the code I wrote:

from guizero import *

red = 255,0,0

def cl_ch():
    if mahe_pushbutton.text == "Push":
        mahe_text.text_colour = red
    else:
        print("Not working")

mahe_app = App(title = "TEST")

mahe_text = Text(mahe_app, text = "test test", font = "Orbitron", size = 20)

mahe_textbox = TextBox(mahe_app, width = 50)



mahe_pushbutton = PushButton(mahe_app, command=cl_ch, text = "Push")

mahe_pushbutton.width = 60

mahe_pushbutton.height = 3



mahe_app.display()

Thanks if you are able to help!

Upvotes: 1

Views: 940

Answers (2)

Pedro Vicente
Pedro Vicente

Reputation: 1

from guizero import *

red = (255,0,0)



mahe_app = App(title = "TEST")

mahe_text = Text(mahe_app, text = "test test", font = "Orbitron", size = 20)


def cl_ch():
    mahe_text.text_color = red

def refresh():
    mahe_text.text_color = "black"



mahe_textbox = TextBox(mahe_app, width = 50)



mahe_pushbutton = PushButton(mahe_app, command=cl_ch, text = "Push")

mahe_pushbutton.width = 60

mahe_pushbutton.height = 3


mahe_app.repeat(250, refresh)
mahe_app.display()

You need red = (255,0,0) instead of red = 255,0,0 the function must be declared after the Text or receive the Text as a entry arg. If you never change the text in the button, remove the condition because it will always enter in the if and never in the else. I didn't know if you wanted the text to turn black again so i createda function that puts the text black and used the method repeat from the App to put the text black every 250 milisseconds

Upvotes: 0

Illusjason
Illusjason

Reputation: 26

Hi I've only just started looking at Guizero, my programming skills aren't very advanced but This should make the text red when you press the button but stays red afterwards.

from guizero import *

red = 255,0,0

def cl_ch():        

    mahe_text.text_color = "red"


mahe_app = App(title = "TEST")

mahe_text = Text(mahe_app, text = "test test", font = "Orbitron", size = 20)

mahe_textbox = TextBox(mahe_app, width = 50)

mahe_pushbutton = PushButton(mahe_app, command=cl_ch, text = "Push")

mahe_pushbutton.width = 60

mahe_pushbutton.height = 3

mahe_app.display()

This next bit crudely resets the text to black after about a second or 1000 miliseconds

from guizero import *

red = 255,0,0

def cl_ch():        

    mahe_text.text_color = "red"
    
def cl_ch_2():

    mahe_text.text_color = "black"

mahe_app = App(title = "TEST")

mahe_text = Text(mahe_app, text = "test test", font = "Orbitron", size = 20)

mahe_textbox = TextBox(mahe_app, width = 50)

mahe_text.repeat(1000,cl_ch_2) # a kind of counter that reset the colour back to black

mahe_pushbutton = PushButton(mahe_app, command=cl_ch, text = "Push")

mahe_pushbutton.width = 60

mahe_pushbutton.height = 3

mahe_app.display()

When you push the button it runs the cl_ch function so you just need to update that one attribute or method of the Text which is the text_color.

I can't figure out how to set it back though so I just coded a repeat text after 1000 miliseconds as a reset.

hope that helped

Upvotes: 1

Related Questions