python - 在 python/kivy 中使用 screenmananger 时出现黑屏
问题描述
我正在尝试使用 kivy 和 python 构建一个应用程序。当我实现屏幕管理器时,我得到一个黑屏。正如您在我的代码中看到的,我有两个屏幕:ApppScreen 和 AllitemsScreen。我想让我的 ApppScreen 首先作为“主屏幕”出现。出了什么问题?提前致谢!
.kv 文件:
<ApppScreen>:
Label:
text: 'hoi'
BoxLayout:
size: root.width, root.height
orientation: 'vertical'
Label:
text: 'hey'
size_hint: 1, .1
ScrollView:
size_hint: 1, .3
GridLayout:
size_hint: 1,None
height: self.minimum_height
cols: 1
MyLabel:
text: ' items'
GridLayout:
id: container
cols: 2
size_hint_y: None
height: self.minimum_height
row_force_default: True
row_default_height: dp(260)
TextInput:
title: 'NewItem'
id: input
multiline: False
size_hint: 1, .1
Button:
text: 'add new item'
size_hint: 1, .1
on_press: app.add_a_row()
Button:
text: 'All items'
size_hint: 1, .1
on_press: root.manager.current = 'allitems'
<AllitemsScreen>:
BoxLayout:
size: root.width, root.height
orientation: 'vertical'
Label:
text: 'hey'
size_hint: 1, .1
ScrollView:
size_hint: 1, .3
GridLayout:
size_hint: 1,None
height: self.minimum_height
cols: 1
MyLabel:
text: 'All items'
GridLayout:
size_hint: 1,None
height: self.minimum_height
cols: 2
rows: 25
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
TextInput:
title: 'NewItem'
id: input
multiline: False
size_hint: 1, .1
Button:
text: 'add new item'
size_hint: 1, .1
on_press: root.newitem(input.text)
Button:
text: 'items2'
size_hint: 1, .1
on_press: root.manager.current = 'appp'
<TextInp>:
title: 'NewItem'
id: test1
auto_dismiss: False
TextInput:
multiline: False
id: input
hint_text:'Enter the items url'
on_text: app.NewItem()
<Row>:
orientation: "horizontal"
Image:
source: app.image_source
width: 100
main.py 文件
import kivy
kivy.require('1.9.0')
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix import image
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from price import ExtractPrice
from pythonmain import Item
from image import ExtractImage
import requests
from urllib.request import urlopen
from PIL import Image
from datetime import date
class ApppScreen(Screen):
def newitem(self, link):
item_list.append(Item(link))
print(item_list[0].store, item_list[0].original_price)
class AllitemsScreen(Screen):
pass
sm = ScreenManager()
sm.add_widget(ApppScreen(name='appp'))
sm.add_widget(AllitemsScreen(name='allitems'))
class Row(BoxLayout):
pass
class notifier(App):
image_source = item1.image
print(image_source)
def build(self):
return sm
def add_a_row(self):
self.root.ids.container.add_widget(Row())
if __name__ == '__main__':
notifier().run()
解决方案
在加载 kv 之前创建小部件。而是在 build 方法中创建它们。
推荐阅读
- python - 在另一个列表中添加列表值的出现
- maven - 将多个侦听器添加到 Spring 批处理步骤时出现编译错误
- java - 不通过 http 请求由 Thymeleaf 呈现 Html
- r - 使用 R 中的 ggplot 将数据并排分成多个季节和箱线图?
- python-3.x - If 语句,ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
- c# - 每次打开该表单时从数据库自动更新 Datagridview
- android - 如果只有另一个流已经发出项目并且它没有在 Rxjava 中使用,我该如何发出
- sql - Oracle SQL 创建变量并在另一个查询中使用输出
- go - 为什么 Revel JSON 响应中的所有键都不大写?
- python - 如何在 Python 中将包括双引号在内的单词提取到 LIKE GOOGLE 列表中