首页 > 解决方案 > 在 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()

标签: pythonkivy

解决方案


在加载 kv 之前创建小部件。而是在 build 方法中创建它们。


推荐阅读