Kivy Python: buttons and classes for multiple screens(Kivy Python:用于多屏幕的按钮和类)
本文介绍了Kivy Python:用于多屏幕的按钮和类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Kivy Python代码有问题。我有两个屏幕:第一个是导航到第二个屏幕,在第二个屏幕上有一个按钮来添加文本到滚动视图…导航工作,但它没有添加任何文本到滚动视图…我想我需要一些帮助!AttributeError:‘Super’对象没有属性‘getattr’
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock, mainthread
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.effects.scroll import ScrollEffect
from kivy.lang import Builder
Builder.load_string("""
<MenuScreen>:
name: 'mainmenu'
BoxLayout:
spacing: 1
orientation: "vertical"
Label:
text: "MAIN MENU"
Button:
text: 'Go to Screen 2'
on_release:
root.manager.current = 'screen2'
root.manager.transition.direction = "left"
Button:
text: 'Quit'
on_release: root.manager.current = app.exit_software()
<Screen2>:
name: 'screen2'
BoxLayout:
spacing: 1
orientation: "vertical"
ScrollView:
id: scroll_view
always_overscroll: False
BoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Label:
id: label
text: "You can add some Text here by pressing the button"
size_hint: None, None
size: self.texture_size
Button:
text: 'Add text!'
size_hint_y: 0.1
on_release: app.add_text()
Button:
text: 'Back to main menu'
size_hint_y: 0.1
on_release:
root.manager.current = 'mainmenu'
root.manager.transition.direction = "right"
""")
# Declare both screens
class MenuScreen(Screen):
pass
class Screen2(Screen):
pass
class AddTextApp(App):
def __init__(self,**kwargs):
super().__init__(**kwargs)
def build(self):
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='mainmenu'))
sm.add_widget(Screen2(name='screen2'))
return sm
def add_text(self):
self.root.ids.label.text += f"Some new Text
"
self.root.ids.scroll_view.scroll_y = 0
def exit_software(self):
App.get_running_app().stop()
if __name__ == '__main__':
AddTextApp().run()
提前谢谢您!
推荐答案
发生错误是因为self.root.ids
可以访问位于主类的根小部件中的小部件。要访问辅助屏幕元素,您需要将其添加到主类(在您的示例中,在ScreenManager
中)并设置其id
。此外,您有大量的导入过剩,因此它很明显,我建议您使用PyCharm或类似的工具。
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
kv = """
<MenuScreen>:
name: 'mainmenu'
BoxLayout:
spacing: 1
orientation: "vertical"
Label:
text: "MAIN MENU"
Button:
text: 'Go to Screen 2'
on_release:
root.manager.current = 'screen2'
root.manager.transition.direction = "left"
Button:
text: 'Quit'
on_release: root.manager.current = app.exit_software()
<Screen2>:
name: 'screen2'
BoxLayout:
spacing: 1
orientation: "vertical"
ScrollView:
id: scroll_view
always_overscroll: False
BoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Label:
id: label
text: "You can add some Text here by pressing the button"
size_hint: None, None
size: self.texture_size
Button:
text: 'Add text!'
size_hint_y: 0.1
on_release: app.add_text()
Button:
text: 'Back to main menu'
size_hint_y: 0.1
on_release:
root.manager.current = 'mainmenu'
root.manager.transition.direction = "right"
ScreenManager:
MenuScreen:
id: menu_scr
Screen2:
id: scr_2
"""
class MenuScreen(Screen):
pass
class Screen2(Screen):
pass
class AddTextApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def build(self):
return Builder.load_string(kv)
def add_text(self):
self.root.ids.scr_2.ids.label.text += f"Some new Text
"
self.root.ids.scr_2.ids.scroll_view.scroll_y = 0
@staticmethod
def exit_software():
App.get_running_app().stop()
if __name__ == '__main__':
AddTextApp().run()
这篇关于Kivy Python:用于多屏幕的按钮和类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Kivy Python:用于多屏幕的按钮和类
基础教程推荐
猜你喜欢
- Python h5py-为什么我收到广播错误? 2022-09-21
- 如何将RPC与Volttron配合使用 2022-09-21
- 在 pandas 中使用带有多重索引的.loc 2022-09-22
- 使用工作区API导入方法导入数据库笔记本(动态内 2022-09-21
- 从顶点坐标创建三角网格 2022-09-21
- 如何防止Groupby超越指数? 2022-09-22
- 如何在hdf5文件的多个组之间拆分数据? 2022-09-21
- 在OpenCV中放大后,Python会捕捉图像的特定部分 2022-09-22
- 跟在带量词的前瞻后面有什么作用? 2022-09-22
- 获取多索引中某个级别的最后一个元素 2022-09-22