Adding a widget in the root center

9 views
Skip to first unread message

Sergey Vdovin

unread,
Nov 21, 2018, 4:00:24 AM11/21/18
to Kivy users support
Hello.
I'm confused with a stupid problem. I'm trying to make a draggable widget that is similar to Popup and Scatter.

Expected functionality:
User press button "Search tab" - yellow draggable widget appears in the center of the window.

Problem:
The yellow widget appears in the left low corner.

I used a lot of different ways to handle the issue but no method succeded. Probably, I'm missing something very simple.

How would you solve it?

Main file - tmp3.py
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.boxlayout import BoxLayout
from tmp4 import tmpWidget

Builder.load_string('''

<tmpWidget2>:
orientation: 'vertical'
Button:
text: 'Search tab'
size_hint: 1, None
height: 40
on_press: root.search()
Label:
text: ''
size_hint: 1, 1

''')

class tmpWidget2(BoxLayout):
tmp = tmpWidget()
def search(self):
self.add_widget(self.tmp)

class Gui2App(App):
def build(self):
self.prog = tmpWidget2()
return self.prog
if __name__ == '__main__':
Gui2App().run()


Imported file - tmp4.py
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.behaviors import DragBehavior

Builder.load_string('''

<tmpWidget>:
size_hint: None, None
size: 250,50
drag_rectangle: self.x, self.y, self.width, self.height
    drag_timeout: 10000000
    drag_distance: 0
BoxLayout:
orientation: 'vertical'
size_hint: None, None
size: 250,50
spacing: 5
padding: 5
canvas.before:
Color:
rgb:  1,1,0
Rectangle:
size: self.size
pos: self.pos
BoxLayout:
orientation: 'horizontal'
Label:
text: ''
size_hint: None, None
width: 20
height: 40
Label:
text: 'SEARCH TAB'
color: 0,0,0,1
size_hint: 1, 1
Button:
text: 'X'
size_hint: None, 1
width: 40
on_press: root.search_exit()
''')

class tmpWidget(DragBehavior, RelativeLayout):
def search_exit(self):
App.get_running_app().prog.remove_widget(App.get_running_app().prog.tmp)



Thank you in advance
tmp3.py
tmp4.py

Sergey Vdovin

unread,
Nov 21, 2018, 6:19:24 AM11/21/18
to Kivy users support
I've made a workaround using FloatLayout instead of BoxLayout. Honestly, it's not the best solution for me. If somebody knows better way, I'll be gratefull listener...

The workaround is in attachment

среда, 21 ноября 2018 г., 12:00:24 UTC+3 пользователь Sergey Vdovin написал:
tmp3.py
tmp4.py
Reply all
Reply to author
Forward
0 new messages