Я пишу python программу для вычисления нескольких значений в двух текстовых входах, но Результат должен быть в таблице ... Я создал ее как метку, но это не практично. Как я могу создать таблицу! Большое спасибо
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
Window.clearcolor = (0.5, 0.5, 0.5, 1)
class WindowManager(ScreenManager):
pass
class BestWindow(Screen):
my_result = StringProperty("")
inpt_one = ObjectProperty(None)
result_layout = ObjectProperty(None)
result_layout1 = ObjectProperty(None)
result_layout2 = ObjectProperty(None)
result_layout5 = ObjectProperty(None)
test3 = ObjectProperty(None)
def __init__(self, **kwargs):
super(BestWindow, self).__init__(**kwargs)
Window.bind(on_key_down=self._on_keyboard_down)
def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
if self.test3.focus and keyboard== 13: # 32 - Cpace key presssed Ascii
self.calculate()
def calculate(self):
self.result_layout.clear_widgets()
self.result_layout1.clear_widgets()
nums_one = self.inpt_one.text.strip().split(" ")
nums_two = self.inpt_two.text.strip().split(" ")
if(len(nums_one) < len(nums_two)):
for _ in range(len(nums_two)-len(nums_one)):
nums_one.append(0)
elif(len(nums_two) < len(nums_one)):
for _ in range(len(nums_one)-len(nums_two)):
nums_two.append(0)
result = [(int(x) + int(y)) for x,y in zip(nums_one, nums_two) ]
aList = []
for f in range(len(nums_one)):
aList.append(f)
self.ids.result_layout5.add_widget(Label(text='Num: {} '.format(f)))
bList = []
for i in range(len(nums_one)):
self.ids.result_layout1.add_widget(Label(text='1.Gas: %{} '.format(nums_one[i])))
bList.append(i)
cList = []
for j in range(len(nums_two)):
bList.append(j)
self.ids.result_layout2.add_widget(Label(text='2.Gas: %{} '.format(nums_two[j])))
dList = []
for res in result:
res = int(res)
dList.append(res)
self.ids.result_layout.add_widget(Label(text='Sum: %{} '.format(res)))
self.inpt_one.text = ''
self.inpt_two.text = ''
kv = Builder.load_file("MyMain.kv")
class TestApp(App):
def build(self):
b1 = WindowManager()
return b1
if __name__ == "__main__":
TestApp().run()
, пользователь должен ввести первое значение в первом поле, затем пробел, затем второе значение и так далее. затем он нажимает клавишу ввода .. и затем может продолжить первое значение во втором поле, затем второе значение.
MyMain.kv file
<CustButton@Button>:
font_size: 40
<WindowManager>:
BestWindow:
<BestWindow>:
name: "erst"
inpt_one: inpt_one
inpt_two: inpt_two
result_layout: result_layout
result_layout1:result_layout1
result_layout2:result_layout2
result_layout5:result_layout5
test3: test3
GridLayout:
spacing: 10
cols:1
Label:
text: "das Gas 1"
background_color: 1, 0, 0, 1
TextInput:
id:inpt_one
focus : True
text: ' '
width: 100
multiline: False
on_text_validate: inpt_two.focus = True
Label:
text: "das Gas 2"
background_color: 1, 0, 0, 1
TextInput:
id:inpt_two
focus : True
text: ' '
width: 100
multiline: False
on_text_validate:
test3.background_normal = ''
test3.background_color = [0, 0, 1, 0.5] # 50% translucent blue
test3.focus = True
Button:
id : test3
focus: False
text: "Table!"
on_press:
root.calculate()
BoxLayout:
orientation: "horizontal"
BoxLayout:
id: result_layout5
orientation: "horizontal"
BoxLayout:
orientation: "horizontal"
BoxLayout:
id: result_layout1
orientation: "horizontal"
BoxLayout:
orientation: "horizontal"
BoxLayout:
id: result_layout2
orientation: "horizontal"
BoxLayout:
orientation: "horizontal"
BoxLayout:
id: result_layout
orientation: "horizontal"