- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 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/
我正在准备在 kivy 中做一个进度条,我可以用它构建应用程序,但是当我运行一个函数(循环)时它不能被更新,我该怎么做? 这是我的代码: 导入库: from kivy.app import App f
为猕猴桃中的按钮创建圆角的首选方法是什么? 还有其他同样可行的方法来执行此任务吗?谢谢。 最佳答案 这是一个棘手的问题。就我而言,Widgets始终是矩形。但是我们可以更改背景,并分别使用backgr
我是kivy的新手。我有一个按钮可以刷新数据库中的列表项,这是绑定(bind)到该按钮的函数: def refresh_account(self): self.ids.grid.clear_w
我正在尝试使用 NumericProperty,但在尝试将其用作值时出现类型错误 我的代码是这样的 from kivy.properties import NumericProperty from k
在按钮中组合图像/图标和文本的首选方法是什么?例如,您将如何使用 text = 'my button' 创建按钮,以及该文本左侧的图形图标? 最佳答案 关于问题#2。 Kivy 的工作方式是嵌入 Wi
在 kivy 中,您如何使用自动居中的多行文本创建按钮或标签?如果你做类似的事情,Button(text = 'my button\nthis is my button') ,似乎只有一条线会居中,而
我知道如何制作彩色背景,但我似乎找不到任何有用的内容将图像设置为背景,并且非常感谢您对我的代码的任何帮助。 这是我的 .py 文件: from kivy.app import App from kiv
我的 Kivy 语言文件有许多 font_size 属性,所有属性都具有相同的值,是否可以在 KV lang 中分配变量? 当前 KV 文件示例: #User ID Label:
我有一个使用许多标签的 kivy 应用程序。是否可以从列表中获取它们的值?例如(但这不起作用) Label: text:root.label_value[0]
任何人都可以帮助我使用任何 sdk 在 kivy 应用程序中实现广告。 Revmobs 已停止支持 Kivy。 任何其他实现广告的方法也可以使用。 谢谢 最佳答案 我在 上取得了成功AdBuddiz
基维 gesture documentation有点缺乏,仅指手势示例。 我想知道为什么 Kivy 不提供任何辅助方法,例如 on_swipe_left、on_swipe_up 等。最好将 minsc
我目前使用的是 python 2.7.9。我试过重新安装 cython 并更新所有依赖项,但它没有用。我不知道 Buildozer 或 Cython 有什么问题。该应用程序直接从终端正常运行。 #er
Kivy 文档指定 "each widget in Kivy already have by default their Canvas" .然而,在实践中,小部件似乎持有对整个窗口的共享 Canvas
我是 Python 库 kivy 的新手。我找到了额外的库 kivy-md,它有非常漂亮的 ui 元素。目前我想从字典变量创建许多 MDTextField 小部件,例如 # text_fields.p
python 3.4基维 1.10.0 我正在尝试使用 Kivy Animation 类来为我的 Image 类制作动画。这是因为我想分别修改每个图像的 anim_delay 和位置值。 我想修改图像
我正在尝试使用 kivy 启动器在我的 android 上启动我的应用程序,这样我就可以在较小的屏幕上看到小部件位置/大小发生了什么。 当我启动时,它崩溃了。 所以...我猜我在我的应用程序中做了一些
有谁知道如何在 Kivy 中增加 MeshLinePlot 的线宽? 谢谢 更新 我从@Ikolim 那里得到了关于修改 kivy.graph 中的 LinePLot 函数的答案 class Line
如何使用 Kivy 更改窗口的大小。我一直在四处寻找,除了进入窗口的大小之外,我几乎可以更改所有内容。 从示例图片文件: 主文件 #!/usr/bin/kivy ''' Pictures demo =
我试图在 Windows 上安装 kivy 设计器。我按照步骤操作,但是当我尝试运行时 python -m designer 我收到以下错误: [INFO ] [Kivy ] v1.
我正在尝试让我的 python 和 kivy 文件打开一个弹出窗口。它说我的 Boxlayout 对象没有属性“open_popup” 这是我的Python代码: from kivy.app impo
我是一名优秀的程序员,十分优秀!