Importing modules and designing interactions

24 views
Skip to first unread message

Sergey Vdovin

unread,
Nov 2, 2018, 7:16:08 AM11/2/18
to Kivy users support
Hello.
I'm making a desktop application and have decided to create a markup layout in the main py file importing all the required widgets in the respective sections in order to make logic cleaner. I stumbled upon some issues:
1. What is the best practice to do it? Supposedly, I'm inventing a bicycle...
2. Why do I get a black screen If i write such a KV text (see below)? tree_widget and textable_widget are imported classes (see the whole code onwards).
<main_window>:
 BoxLayout:
 orientation: 'horizontal'
 tree_widget:
 
 textable_widget:

I can see my imported modules If I add a "standard" widget before:
<main_window>:
 BoxLayout:
 orientation: 'horizontal'
 
 Label:
    text: "Widget that I don't need"
 
 tree_widget:
 
 textable_widget:

Screenshot_2.jpg














2. How can I create interactions between two imported modules in the main application? I want to show, for example, an internal ID of each TreeView element ("num" NumericProperty in tree_class.py) in the right section (textable_widget). Roughly speaking, how to rewrite on_touch_down() method, that is initially used in tree_class.py, - in main.py? I inherit tree_widget class in main.py from tree_window class in tree_class.py but don't know how to get access to on_touch_down() method in tree_class.CustomLabel

main.py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang.builder import Builder
import textable_class
import tree_class


Builder.load_string('''
<main_window>:
 BoxLayout:
 orientation: '
horizontal'
 padding: 10
 spacing: 10
 
 Label:
 text: "Widget that I don'
t need"
 
 tree_widget:
 
 textable_widget:
''')


class tree_widget(tree_class.tree_window):
 pass


class textable_widget(textable_class.textable_widget):
 pass


class main_window(FloatLayout):
 pass


class mainApp(App):
 title = "
Sandbox"
 
 def build(self):
 return main_window()


if __name__ == '__main__':
 mainApp().run()

tree_class.py
from kivy.app import App
from kivy.uix.treeview import TreeView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.treeview import TreeViewLabel
from kivy.properties import NumericProperty


class CustomLabel(TreeViewLabel):
 num
= NumericProperty()
 
def on_touch_down(self, touch):
 
#do something
 
pass


class tree_window(BoxLayout):
 
 
def __init__(self, **kwargs):
 
super(tree_window, self).__init__(**kwargs)
 tree
= TreeView(root_options=dict(text='Tree Widget'))
 
for counter in range(1,11):
 name
= 'Label ' + str(counter)
 tree
.add_node(CustomLabel(text=name, num=counter))
 
self.add_widget(tree)


class tree_classApp(App):
 
 
def build(self):
 
return tree_window()


if __name__ == '__main__':
 tree_classApp
().run()


textable_class.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty


Builder.load_string('''
<textable_widget>:
 orientation: '
vertical'
 
 Label:
 id: var_label
 text: '
Variable text'
'''
)


class textable_widget(BoxLayout):
 
pass


class textable_classApp(App):
 
 
def build(self):
 
 
return textable_widget()


if __name__ == '__main__':
 textable_classApp
().run()

Thank you in advance
main.py
textable_class.py
tree_class.py
Message has been deleted

Глеб Самойлов

unread,
Nov 2, 2018, 8:20:09 AM11/2/18
to Kivy users support
Why so complicate?
main.py
textable_class.py
tree_class.py
ezgif.com-video-to-gif.gif

Sergey Vdovin

unread,
Nov 2, 2018, 12:49:16 PM11/2/18
to Kivy users support
Thank you. There're number of precise decisions in your code. I'm in programming just few months so I don't see the most appropriate way to solve a problem. I gathered several good tricks from you.
Generally, I'm working on a GUI where seven widgets work. I tried to describe it one class but get stuck. Probably, separation into several files is easier. Thank you for your help in designing it.

пятница, 2 ноября 2018 г., 15:20:09 UTC+3 пользователь Глеб Самойлов написал:
Why so complicate?
Reply all
Reply to author
Forward
0 new messages