如何在不使用 kv 文件的情况下使用 python 中的 for 循环将 id 设置为标签?
How to set id's to labels using for loop in python without using the kv file?
我已经想出如何将多个标签添加到 python 文件中的网格,但我正在努力将 ID 添加到 for 循环中的这些标签。我尝试了以下但似乎不起作用。还请指导我使用 ID 访问这些标签。非常感谢任何帮助或指导。谢谢
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty
Builder.load_file("gridtable.kv")
class MyBox(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
pass
# for i in range(48):
# self.b = TextInput(multiline=False, font_size=dp(30))
# self.mygrid.add_widget(self.b)
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols=6
self.textinputs = {}
for i in range(48):
key = i+1
self.textinputs[key] = TextInput(multiline=False,font_size=dp(30),on_text=self.calc(key))
self.add_widget(self.textinputs[key])
def calc(self,key):
print(self.textinputs[key])
class MyApp(App):
def build(self):
return MyBox()
if __name__ == "__main__":
MyApp().run()
<MyBox>:
mygrid:my_grid
orientation: "vertical"
MyGrid:
id: my_grid
size_hint: 1,0.8
BoxLayout:
orientation: "horizontal"
size_hint: 1,0.2
BoxLayout:
orientation: "vertical"
Button:
text: "Expense Total:"
Button:
text: "Revenue Total:"
Button:
text: "Profit:"
font_size: 40
Traceback (most recent call last):
File "c:\Users\jaika\OneDrive\Desktop\python\lil_curry_project\gridtable.py", line 38, in <module>
MyApp().run()
print(self.textinputs[key])
KeyError: 1
I tried the following but that doesn't seem to work.
当某些东西不起作用时,您应该 post how/why 它失败的详细信息。例如,它是否因回溯而崩溃?
Please also guide me to access those labels using the ids
kivy 使用的 id
只能通过 kv 语言设置,它在设置它的 kv 规则中具有特定的本地行为。
由于您是在 python 中创建小部件,因此您不需要设置 ID,您可以将它们存储在列表或字典或您喜欢的任何其他内容中。例如,您可以设置 self.textinputs = {}
然后为每个 self.textinputs[i] = TextInput(...); self.add_widget(self.textinputs[i])
.
我已经想出如何将多个标签添加到 python 文件中的网格,但我正在努力将 ID 添加到 for 循环中的这些标签。我尝试了以下但似乎不起作用。还请指导我使用 ID 访问这些标签。非常感谢任何帮助或指导。谢谢
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty
Builder.load_file("gridtable.kv")
class MyBox(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
pass
# for i in range(48):
# self.b = TextInput(multiline=False, font_size=dp(30))
# self.mygrid.add_widget(self.b)
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols=6
self.textinputs = {}
for i in range(48):
key = i+1
self.textinputs[key] = TextInput(multiline=False,font_size=dp(30),on_text=self.calc(key))
self.add_widget(self.textinputs[key])
def calc(self,key):
print(self.textinputs[key])
class MyApp(App):
def build(self):
return MyBox()
if __name__ == "__main__":
MyApp().run()
<MyBox>:
mygrid:my_grid
orientation: "vertical"
MyGrid:
id: my_grid
size_hint: 1,0.8
BoxLayout:
orientation: "horizontal"
size_hint: 1,0.2
BoxLayout:
orientation: "vertical"
Button:
text: "Expense Total:"
Button:
text: "Revenue Total:"
Button:
text: "Profit:"
font_size: 40
Traceback (most recent call last):
File "c:\Users\jaika\OneDrive\Desktop\python\lil_curry_project\gridtable.py", line 38, in <module>
MyApp().run()
print(self.textinputs[key])
KeyError: 1
I tried the following but that doesn't seem to work.
当某些东西不起作用时,您应该 post how/why 它失败的详细信息。例如,它是否因回溯而崩溃?
Please also guide me to access those labels using the ids
kivy 使用的 id
只能通过 kv 语言设置,它在设置它的 kv 规则中具有特定的本地行为。
由于您是在 python 中创建小部件,因此您不需要设置 ID,您可以将它们存储在列表或字典或您喜欢的任何其他内容中。例如,您可以设置 self.textinputs = {}
然后为每个 self.textinputs[i] = TextInput(...); self.add_widget(self.textinputs[i])
.