Using Class in Kivy
Good afternoon, I want to make a program that uses multiple combobox and in one window. But I can't get them both to show up. When I run the program, only one of the combobox appears. Below I send the current code.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.properties import ListProperty
from kivy.core.window import Window
class ComboPotenciaConsumo(BoxLayout):
textoEntradaPotenciaConsumo = StringProperty()
opcoesPotenciaConsumo = ListProperty()
valorInicialPotenciaConsumo = StringProperty()
def __init__(self, **kwargs):
super(ComboPotenciaConsumo, self).__init__(**kwargs)
self.opcoesPotenciaConsumo = ['Potência', 'Consumo']
self.valorInicialPotenciaConsumo = 'Potência'
def print_txt1(self):
if self.textoEntradaPotenciaConsumo == 'Potência':
a = 1
elif self.textoEntradaPotenciaConsumo == 'Consumo':
a = 2
print('{}'.format(a))
class ComboPotenciaPainel(BoxLayout):
textoEntradaPotenciaPainel = StringProperty()
opcoesPotenciaPainel = ListProperty()
valorInicialPotenciaPainel = StringProperty()
def __init__(self, **kwargs):
super(ComboPotenciaPainel, self).__init__(**kwargs)
self.opcoesPotenciaPainel =['275 Wp', '330 Wp', '360 Wp']
def print_txt2(self):
if self.textoEntradaPotenciaPainel == '275 Wp':
potenciaPainel = 275
elif self.textoEntradaPotenciaPainel == '330 Wp':
potenciaPainel = 330
elif self.textoEntradaPotenciaPainel == '360 Wp':
potenciaPainel = 360
print('{}'.format(potenciaPainel))
class ProgramaOrcamentoKivy(App):
def build(self):
return ComboPotenciaConsumo()
return ComboPotenciaPainel()
if __name__ == '__main__':
Window.size = (1000, 300)
ProgramaOrcamentoKivy().run()
Below I also send the kv file
<ComboPotenciaConsumo>:
opcoesPotenciaConsumo: spinner_1.values
valorInicialPotenciaConsumo: spinner_1.text
orientation: 'vertical'
Label:
text: 'Orcamentos 1.1'
font_size: '40sp'
bold: True
color: [1, 0, 0, 1]
height: '32dp'
GridLayout:
cols: 2
spacing: 20
padding: [20,20]
Button:
on_press: root.print_txt1()
text: 'Button 1'
id: but_1
size_hint: [1, None]
height: '32dp'
Spinner:
id: spinner_1
values: root.opcoesPotenciaConsumo
size_hint: [0.5, None]
height: '32dp'
on_text: root.textoEntradaPotenciaConsumo = self.text
:
opcoesPotenciaConsumo: spinner_2.values
valorInicialPotenciaConsumo: spinner_2.text
orientation: 'vertical'
Label:
text: 'Orcamentos 1.1'
font_size: '40sp'
bold: True
color: [1, 0, 0, 1]
height: '32dp'
GridLayout:
cols: 2
spacing: 20
padding: [20,20]
Button:
on_press: root.print_txt2()
text: 'Button 2'
id: but_2
height: '32dp'
Spinner:
id: spinner_2
values: root.opcoesPotenciaPainel
size_hint: [8, None]
height: '32dp'
on_text: root.textoEntradaPotenciaPainel = self.text
1
1 answers
This is your function
def build(self):
return ComboPotenciaConsumo()
return ComboPotenciaPainel()
Has two return
- but when python Finds return
it terminates the function, so the second return
will never be executed!
Thus, the second combo is not being created because of this.
There are several ways to solve, depending on how you want to format your application. One of them is to create a layout and put the two combos in the layout, then use a return
only:
def build(self):
c1 = ComboPotenciaConsumo()
c2 = ComboPotenciaPainel()
l = BoxLayout(padding=5)
l.add_widget(c1)
l.add_widget(c2)
return l
0
Author: nosklo, 2018-10-03 20:12:07