DearPyGui icon indicating copy to clipboard operation
DearPyGui copied to clipboard

change image of add_image_button()

Open iolpltuisciaoln opened this issue 2 years ago • 6 comments

Hello, is there a way to change image of an image button (add_image_button()) like when toggling it? Thanks, sorry couldn't googled an answer.

iolpltuisciaoln avatar May 06 '23 09:05 iolpltuisciaoln

like maybe updating texture_tag ?

iolpltuisciaoln avatar May 07 '23 14:05 iolpltuisciaoln

Hello. Can you give some simple working example to add button with image (icon.png) for example? I can`t make it working. Errors..

a1ex3d avatar Jun 06 '23 13:06 a1ex3d

import dearpygui.dearpygui as dpg

class State:
    def __init__(self):
        self.button_state_on = True

S = State()

Textures = {}
dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

def toggle_button():
    if S.button_state_on:
        dpg.set_value("ch1_on", Textures["CH1_off"][3])
    else:
        dpg.set_value("ch1_on", Textures["CH1_on"][3])
    S.button_state_on = not S.button_state_on

with dpg.window(label="Example Window"):
    with dpg.texture_registry(show=False):
        width, height, channels, data = dpg.load_image("icons/ch1_1.png")
        Textures["CH1_on"] = [width, height, channels, data]
        dpg.add_dynamic_texture(width=width,
                                    height=height,
                                    default_value=data,
                                    tag="ch1_on")

        width, height, channels, data = dpg.load_image("icons/ch1_0.png")
        Textures["CH1_off"] = [width, height, channels, data]
        dpg.add_dynamic_texture(width=width,
                                    height=height,
                                    default_value=data,
                                    tag="ch1_off")

    dpg.add_image_button(texture_tag="ch1_on",
                                        label="CH1",
                                        width=200,
                                        height=100,
                                        frame_padding=0,
                                        callback=toggle_button,
                                        user_data="")

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

iolpltuisciaoln avatar Jun 06 '23 13:06 iolpltuisciaoln

note that this is a bit hacky method, what it does is overwrites preloaded texture "ch1_on" with raw data every toggle. i could not find a way to update image_button texture during runtime and i think this is either a misconception or my dumb dumb.

iolpltuisciaoln avatar Jun 06 '23 13:06 iolpltuisciaoln

Hello! I think this feature example can be added to the document, such as selecting a file directory through the File Explorer. After importing the window, buttons can be used to control the display of the previous and next images. This is very necessary in my work, and of course, your documentation is also great. Through demo, I can easily combine the functions together, which greatly simplifies my UI development time.

Auorui avatar Jul 22 '23 02:07 Auorui

我明白你的意思,你想在点击按钮后使按钮的图片发生改变,我的也有同样的需求,我的解决方案如下 dpg.add_static_texture(width=width_, height=height_, default_value=data_, ) # 为隐式按钮创建注册表 button_on_close = dpg.add_static_texture(width=width, height=height, default_value=data, ) dpg.add_image_button(texture_tag=button_on_close, tag=pic.replace('.png', ''), background_color=[51, 51, 55, 255], parent=button_box, frame_padding=0, callback=lambda s, a, u: switchingstates(s, u), user_data=0) # user_data用于记录按钮激活状态 def switchingstates(s, u): # 这里利用同一个按钮,两个状态下的图片的id相差1,对id进行加减1进行按钮状态的切换 if u == 0: # 0表示未激活,1表示激活 dpg.configure_item(s, texture_tag=dpg.get_item_configuration(s)["texture_tag"] - 1, user_data=1) else: dpg.configure_item(s, texture_tag=dpg.get_item_configuration(s)["texture_tag"] + 1, user_data=0) recording

LiyueAnd avatar Jul 08 '24 06:07 LiyueAnd