'Python / Kivy: Move one button to the position of other button after confirmation
I want the alpha button to move to the position of the beta button, after confirming first the alpha button and then selecting the position of the beta button.
The different layouts should stay because I need them to be on separate ones. And it should be repeatable with new buttons without needing a new function for each new one.
I know there is quite a lot not right below, but don't know how to solve it.
Thank you for reviewing.
from kivy.app import App
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.core.text import LabelBase
from kivy.config import Config
Config.set('graphics', 'window_state', 'maximized')
kv = ("""
FloatLayout:
ScreenManager:
id: screen_manager
TestScreen:
<TestScreen>:
name: "test_screen"
FloatLayout:
Button:
id: beta
background_color: 1, 0, 0, 1
size_hint: 0.1, 0.2
pos_hint: {'center_x':.95, 'center_y':.1}
on_release: app.beta_active()
on_release: app.move_to()
FloatLayout:
Button:
id: alpha
background_color: 0, 1, 0, 1
size_hint: 0.1, 0.2
pos_hint: {'center_x':.05, 'center_y':.1}
on_release: app.alpha_active()
""")
class TestScreen(Screen):
pass
class TestApp(App):
def build(self):
kivy_design = Builder.load_string(kv)
return kivy_design
def alpha_active(self):
alpha_pos = self.root.ids.screen_manager.get_screen("test_screen").ids.alpha.pos_hint
return alpha_pos
def beta_active(self):
beta_pos = self.root.ids.screen_manager.get_screen("test_screen").ids.beta.pos_hint
return beta_pos
def move_to(self):
alpha_pos = beta_pos
TestApp().run()
Solution 1:[1]
You can just modify your move_to() method as:
def move_to(self):
self.root.ids.screen_manager.get_screen("test_screen").ids.alpha.pos_hint =\
self.root.ids.screen_manager.get_screen("test_screen").ids.beta.pos_hint
Note that your alpha_active() and beta_active() methods do not actually do anything. Also, after move_to() is run, the beta button is still there. It's just underneath the alpha Button.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Anderson |
