[SOLVED] How to change the background of the selected textbutton ?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
span4ev
Regular
Posts: 77
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

[SOLVED] How to change the background of the selected textbutton ?

#1 Post by span4ev » Tue Aug 09, 2022 2:05 pm

Hi all.
I have buttons with categories. I click on the button, the category of the product changes and the products of a certain category are displayed on the screen. It's a textbutton because I'm not using an imagebutton.
But I want the button to change its background after clicking. And I can't find any examples of how to do it...
I don't know if the button has a "selected" property so that when I click the button?

That is, it is easily applied in styles,

Code: Select all

stylesort_button:
    background '#904848'
    hover_background '#a52d2d'
but I don't understand how to do it for "selected". And I did not find a method, for example like this

Code: Select all

renpy.selected_button(background...) - something like that.
I'm fine with any approach how to do it.
Then I could apply a function to the button and change its color through python through the loop index, and remove this color from the previously selected button
Last edited by span4ev on Sat Oct 15, 2022 5:00 am, edited 1 time in total.

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: How to change the background of the selected textbutton ?

#2 Post by enaielei » Tue Aug 09, 2022 2:23 pm

You can just use variables for this.

Code: Select all

screen test():
  default selected = -1
  hbox:
    for i in range(3):
      textbutton "[i]":
        action SetScreenVariable("selected", i)
        background (None if selected != i else "#f00")

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1882
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to change the background of the selected textbutton ?

#3 Post by Ocelot » Tue Aug 09, 2022 2:43 pm

span4ev wrote:
Tue Aug 09, 2022 2:05 pm
but I don't understand how to do it for "selected".
selected_background '#999'
< < insert Rick Cook quote here > >

span4ev
Regular
Posts: 77
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How to change the background of the selected textbutton ?

#4 Post by span4ev » Tue Aug 09, 2022 4:12 pm

Ocelot wrote:
Tue Aug 09, 2022 2:43 pm
span4ev wrote:
Tue Aug 09, 2022 2:05 pm
but I don't understand how to do it for "selected".
selected_background '#999'
I tried this option. This does not work

Code: Select all

for btn in Shop.categories:  

    textbutton btn:
        style 'bbbb' 
        action NullAction()

style bbbb:
    background          '#111'
    hover_background    '#333'
    selected_background '#637999'
 

Code: Select all

for btn in Shop.categories:  
    textbutton btn:
        background '#e43b3b'
        hover_background    '#333'
        selected_background '#637999'
        action NullAction()


span4ev
Regular
Posts: 77
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How to change the background of the selected textbutton ?

#5 Post by span4ev » Tue Aug 09, 2022 4:25 pm

enaielei wrote:
Tue Aug 09, 2022 2:23 pm
You can just use variables for this.

Code: Select all

screen test():
  default selected = -1
  hbox:
    for i in range(3):
      textbutton "[i]":
        action SetScreenVariable("selected", i)
        background (None if selected != i else "#f00")
this is amazing. Where do you find such examples? I've been googling all day but haven't found a single example and I'm getting desperate. I already thought it was simply impossible.
Thanks a lot!

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1882
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to change the background of the selected textbutton ?

#6 Post by Ocelot » Tue Aug 09, 2022 4:29 pm

span4ev wrote:
Tue Aug 09, 2022 4:12 pm
Ocelot wrote:
Tue Aug 09, 2022 2:43 pm
span4ev wrote:
Tue Aug 09, 2022 2:05 pm
but I don't understand how to do it for "selected".
selected_background '#999'
I tried this option. This does not work

Code: Select all

for btn in Shop.categories:  

    textbutton btn:
        style 'bbbb' 
        action NullAction()

style bbbb:
    background          '#111'
    hover_background    '#333'
    selected_background '#637999'
 

Code: Select all

for btn in Shop.categories:  
    textbutton btn:
        background '#e43b3b'
        hover_background    '#333'
        selected_background '#637999'
        action NullAction()

Button in your code will never be selected, as it lacks any action or property that makes it selected. The act of clicking a button does not make it selected. The button is considered selected if either (1) all actions report that running them will not change game state or (2) you set selected property for a button with condition which evaluated to True.

You need to either use action which properly defines selected method (usually subset of Set*** actions) or use selected property with some condition.
< < insert Rick Cook quote here > >

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: How to change the background of the selected textbutton ?

#7 Post by enaielei » Tue Aug 09, 2022 7:53 pm

span4ev wrote:
Tue Aug 09, 2022 4:25 pm
enaielei wrote:
Tue Aug 09, 2022 2:23 pm
You can just use variables for this.

Code: Select all

screen test():
  default selected = -1
  hbox:
    for i in range(3):
      textbutton "[i]":
        action SetScreenVariable("selected", i)
        background (None if selected != i else "#f00")
this is amazing. Where do you find such examples? I've been googling all day but haven't found a single example and I'm getting desperate. I already thought it was simply impossible.
Thanks a lot!
Unfortunately, things like this are undocumented.
You'll learn it as you go.
My tip is to have a sense of understanding about the following.
Screens: https://www.renpy.org/doc/html/screens.html
Actions: https://www.renpy.org/doc/html/screen_actions.html
Style Properties: https://www.renpy.org/doc/html/style_properties.html
Bookmark these links and if you're lost just check them again.

span4ev
Regular
Posts: 77
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How to change the background of the selected textbutton ?

#8 Post by span4ev » Wed Aug 10, 2022 5:46 am

enaielei wrote:
Tue Aug 09, 2022 7:53 pm
span4ev wrote:
Tue Aug 09, 2022 4:25 pm
enaielei wrote:
Tue Aug 09, 2022 2:23 pm
You can just use variables for this.

Code: Select all

screen test():
  default selected = -1
  hbox:
    for i in range(3):
      textbutton "[i]":
        action SetScreenVariable("selected", i)
        background (None if selected != i else "#f00")
this is amazing. Where do you find such examples? I've been googling all day but haven't found a single example and I'm getting desperate. I already thought it was simply impossible.
Thanks a lot!
Unfortunately, things like this are undocumented.
You'll learn it as you go.
My tip is to have a sense of understanding about the following.
Screens: https://www.renpy.org/doc/html/screens.html
Actions: https://www.renpy.org/doc/html/screen_actions.html
Style Properties: https://www.renpy.org/doc/html/style_properties.html
Bookmark these links and if you're lost just check them again.
It's a pity...
I didn't even know that I could use this syntax and write "if/else" in one line!, in a button, for its "background". It's just amazing! I did not expect such syntax.
When people help me (like you) and write such examples, I have no idea where they saw this particular example, how they came up with this. When I need something non-standard, people tell me "guy, go read the documentation", but there is never what I need. And always the only option is to ask a question on the forum, because all non-standard solutions are stored in the minds of people who help, but they are never in the documentation.

Thanks again. Good luck to you!

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: How to change the background of the selected textbutton ?

#9 Post by enaielei » Wed Aug 10, 2022 7:15 am

span4ev wrote:
Wed Aug 10, 2022 5:46 am
It's a pity...
I didn't even know that I could use this syntax and write "if/else" in one line!
That part is actually a Python thing called ternary operation and is just a syntactic sugar. The rest are Ren'Py related.
If you want you can use normal conditionals as well if you're feeling overwhelmed.

Code: Select all

screen test():
  default selected = -1
  hbox:
    for i in range(3):
      python:
        if selected != i:
          bg = None
        else:
          bg = "#f00"
    
      textbutton "[i]":
        action SetScreenVariable("selected", i)
        background bg

span4ev
Regular
Posts: 77
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How to change the background of the selected textbutton ?

#10 Post by span4ev » Wed Aug 10, 2022 4:30 pm

enaielei wrote:
Wed Aug 10, 2022 7:15 am
span4ev wrote:
Wed Aug 10, 2022 5:46 am
It's a pity...
I didn't even know that I could use this syntax and write "if/else" in one line!
That part is actually a Python thing called ternary operation and is just a syntactic sugar. The rest are Ren'Py related.
If you want you can use normal conditionals as well if you're feeling overwhelmed.

Code: Select all

screen test():
  default selected = -1
  hbox:
    for i in range(3):
      python:
        if selected != i:
          bg = None
        else:
          bg = "#f00"
    
      textbutton "[i]":
        action SetScreenVariable("selected", i)
        background bg
No, on the contrary, I feel great looking at this syntactic sugar. I'm glad I saw this example. I'm sorry that I didn't see this earlier and that such things can't be found on Google and it's not written in the documentation. Usually I search for some solution in Google for 1-2 days and can't find it. The only thing I can count on is this forum.
I just didn't know that conditions can be used in properties of the "background" type.

Perhaps the community can band together and write their own documentation, because the official documentation seems to give only half the information about Renpy

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]