gpt4 book ai didi

python - 如何创建带有复选框的动态 kivy 应用程序?

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:18 24 4
gpt4 key购买 nike

我正在使用 kivy 和 python 为我的大学做一个项目。我想做一个主屏幕,您可以在其中选择一些字段,然后下一个屏幕将取决于您在主屏幕中选择的字段。用 kv 语言进行动态对我来说很困难,因为我对 kivy 编程不太了解,所以我决定像 python 编程那样做,但它不起作用。这是我的程序的代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

Builder.load_string('''
<Root>:
MainScreen:
name: 'main'
AnotherScreen:
name: 'another'

<MainScreen>:
GridLayout:
cols: 2
Label:
text: "Select Subjects"
font_size: 15
Label:
text: " "
CheckBox:
on_active:root.ping('ch1')
Label:
text: "Termotecnia"
CheckBox:
on_active:root.ping('ch2')
Label:
text: "Control"
CheckBox:
on_active:root.ping('ch3')
Label:
text: "Termotcnia"
CheckBox:
on_active:root.ping('ch4')
Label:
text: "Control"
Button:
text: "Exit"
background_color: .7, .7, 6, 1
on_release:root.parent.current='another'
Button:
text: "Run"
font_size: 24
background_color: .7, .7, 1, 1
on_release: root.parent.current='another'


''')


class MainScreen(Screen):
def __init__(self, **kw):
super(MainScreen, self).__init__(**kw)
self.a = App.get_running_app()
self.e={}
def ping(self,n):
if self.a.big_dict[n]=='False':
self.a.big_dict[n]='True'
else:
self.a.big_dict[n]='False'
print self.a.big_dict
self.e=self.a.big_dict

class AnotherScreen(GridLayout):
def __init__(self, **kw):
super(AnotherScreen, self).__init__(**kw)
t=[]
self.i=MainScreen()
self.cols=2
self.add_widget(Button(text='Assignatura',background_color=[0,1,1,1]))
self.add_widget(Button(background_color=[0,1,1,1],text='Escriu Grup'))
for k in self.i.e:
if self.i.e[k]=='True':
self.add_widget(Label(text=k))
self.k=TextInput(multiline=False)
t.append(self.k.text)
self.add_widget(self.k)
else:
pass
b1=Button(text='Exit',background_color=[0,1,0,1])
self.add_widget(b1)
b2=Button(text='Run',background_color=[0,1,0,1])
self.add_widget(b2)
b1.bind(on_press=exit)


class Root(ScreenManager):
pass
class SimpleKivy(App):
big_dict={'ch1':'False','ch2':'False','ch3':'False','ch4':'False'}
def build(self):
return Root()
SimpleKivy().run()

此代码不起作用,因为 ScreenManager 仅适用于 Screen,而不适用于 GridLayout(第一个问题)。 big_dict 保存复选框的数据,但是当我尝试导入到另一个类时,它不起作用(另一个问题)。如果有人知道这些问题的答案,那将会非常有帮助,因为我知道如何处理例如总是 13 个字段,但我想进行预选,因为字段太多(在这个程序中只有 4 个,但有 14 个)。

最佳答案

从哪里开始...首先我们看SimpleKivy 。您可能想要 big_dict一个DictProperty ,而不是类属性:

class SimpleKivy(App):
big_dict = DictProperty({'ch1':False,'ch2':False,'ch3':False,'ch4':False})

我还将这些值更改为 bool 值,因为稍后会更有用。

然后,我们为 AnotherScreen 添加另一条规则到 kv 字符串:

<AnotherScreen>:
GridLayout:
id: container
cols: 2

这样,我们就可以通过id来引用实际的布局。 .

我们还更改了 ping 的定义:

def ping(self, n, value):
self.a.big_dict[n] = value

当我们也更改 CheckBox 时es 在 kv字符串到

CheckBox:
on_active:root.ping('ch1', self.active)

复选框的状态将直接传输到应用实例中的字典中。 (或者,如果您不需要 ping 来做其他事情,可以将其简化为 on_active: app.big_dict['ch3'] = self.active )

最后是AnotherScreen的定义:

class AnotherScreen(Screen):
def on_pre_enter(self, *args):
t=[]
a = App.get_running_app()
self.ids.container.add_widget(Button(text='Assignatura',background_color=[0,1,1,1]))
self.ids.container.add_widget(Button(background_color=[0,1,1,1],text='Escriu Grup'))
for k,v in a.big_dict.iteritems():
if v:
self.ids.container.add_widget(Label(text=k))
self.k=TextInput(multiline=False)
t.append(self.k.text)
self.ids.container.add_widget(self.k)
b1=Button(text='Exit',background_color=[0,1,0,1])
self.ids.container.add_widget(b1)
b2=Button(text='Run',background_color=[0,1,0,1])
self.ids.container.add_widget(b2)
b1.bind(on_press=exit)

这在进入屏幕之前调用,将直接从应用实例获取字典(因此不需要引用 MainScreen ),并迭代所述字典。

关于python - 如何创建带有复选框的动态 kivy 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36018476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com