Change Background color of ui.joystick
#3531
-
QuestionIn my small app I am using a joystick. I also have a switch for dark-mode. Now i would like to change the background color of the joystick depending on the setting. Within the browser inspector i found that the following setting changes the background color of the joystick to black: <div data-joystick="" id="c116">
<div style="touch-action: none; background-color: rgb(18, 18, 18);"> <- added background-color here
<div class="nipple collection_0" id="nipple_0_0"
.....
</div>
</div>
</div> However when i use the styling of nicegui: self.joystick.style("background-color: rgb(18, 18, 18);") I get the following html: <div data-joystick="" id="c116" style="background-color: rgb(18, 18, 18);"> <- nicegui adds the style here
<div style="touch-action: none;">
<div class="nipple collection_0" id="nipple_0_0"
.......
</div>
</div>
</div> This however is one level to high and does not apply the color to the nipple objcet. how can i set the syle of the joystick at the correct level within my python code? Thanks in advance! Cheers, |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 13 replies
-
Beta Was this translation helpful? Give feedback.
-
I noticed the css in <style scoped>
:scope > div {
background-color: AliceBlue;
width: 10em;
height: 10em;
position: relative;
}
</style> Then you can use following code to achieve from nicegui import ui
joystick = ui.joystick(color='red',size=50,mode='static').classes('w-96 h-96 bg-white-400 dark:bg-black-400')
ui.query(f'#c{joystick.id} > div').style('background-color:unset;width:100%;height:100%;')
ui.switch('Dark Mode',on_change=lambda e:ui.dark_mode(e.sender.value))
ui.run() |
Beta Was this translation helpful? Give feedback.
-
This question seems related to this feature request: #1707. |
Beta Was this translation helpful? Give feedback.
-
Here is an alternative solution based on @python-and-fiction's approach and Tailwind CSS layers: ui.add_head_html('''
<style type="text/tailwindcss">
@layer components {
.my-joystick div {
@apply !bg-red-100 dark:!bg-green-100;
}
}
</style>
''')
ui.joystick(size=50).classes('my-joystick')
ui.switch('Dark Mode', on_change=lambda e: ui.dark_mode(e.sender.value)) Note that we need to mark these classes as "important" using the exclamation mark to overrule the "AliceBlue" default color. |
Beta Was this translation helpful? Give feedback.
-
Version 1.4.36 with PR #3546 has just been released, allowing to simply set width, height and background color on the |
Beta Was this translation helpful? Give feedback.
I noticed the css in
.venv\Lib\site-packages\nicegui\elements\joystick.vue
make the propertiesbackground-color\width\height
ofdiv
inui.joystick
fixed.Then you can use following code to achieve
ui.joystick
's dark mode by css.