繁体   English   中英

Python-如何在我的奇异应用程序中添加第二个“屏幕”?

[英]Python - How do I add a second “screen” to my kivy application?

注意:我不是程序员,也不知道我应该使用的确切术语。 这是一个基本程序,我不尝试使用文字“奇异画面”或奇异画面管理器。

我已经花了数小时在整个Google上进行搜索以尝试解决此问题,但我很快就会提出来。 我有一个python kivy程序。 我已经创建了初始开始屏幕。 当用户单击特定游戏时,我希望屏幕清除第一个屏幕上的所有数据,然后在屏幕上放置按钮和标签。 几乎就像一个“新”屏幕。 当前,我将其保存到用户选择游戏时清除屏幕的位置,但是,如果我尝试在该屏幕上添加按钮,则会在主屏幕上填充它们。

这是我的python代码:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.dropdown import DropDown

class MadLib(Widget):
    pass

class MadlibApp(App):
    def build(self):
        return MadLib()

app = MadlibApp()

if __name__ == '__main__':
    app.run()

这是我的.kv文件:

<MadLib>:
    canvas:
        Color:
            rgba: 0.2, 0.8, 0.2, 1
        Rectangle:
            pos: 0, 0
            size: self.width, self.height


    Button:
        tag: 'exit'
        background_color: 0, 0, 0, 0
        height: 100
        width: 100
        center_x: root.width - self.width / 2
        top: root.top
        on_press: app.stop()
        Image:
            source: r"C:\Users\kraak\Desktop\Python\Python\basicexitbutton.gif"
            top: root.top
            center_x: root.width - self.width / 2
            size_hint_y: None
            height: '96dp'

    Button:
        tag: 'menu'
        background_color: 0, 0, 0, 0
        id: btn
        top: root.top - 10
        center_x: self.width / 2 + 10
        on_release: dropdown.open(self)
        size_hint_y: None
        height: '48dp'
        width: '175dp'
        Image:
            source: r"C:\Users\kraak\Desktop\Python\Python\basicmenubutton.gif"
            top: root.top + 10
            center_x: self.width / 2
            size_hint_y: None
            height: '96dp'

    Widget:
        on_parent: dropdown.dismiss()

    DropDown:

        id: dropdown
        on_select: btn.text = '{}'.format(args[1])

        Button:
            center_x: self.width / 2
            text: 'Browse'
            font_size: '32'
            background_color: 0, 0 ,0, 0
            size_hint_y: None
            height: '48dp'
            on_release: dropdown.select('A')

        Button:
            text: 'Categories'
            font_size: '32'
            background_color: 0, 0 ,0, 0
            size_hint_y: None
            height: '48dp'
            on_release: dropdown.select('B')

        Button:
            text: 'Settings'
            font_size: '32'
            background_color: 0, 0 ,0, 0
            size_hint_y: None
            height: '48dp'
            on_release: dropdown.select('C')

    Button:
        text: 'Game 1'
        font_size: '32'
        background_color: 0, 0 ,0, 0
        size_hint_y: None
        height: '48dp'
        top: root.top / 1.33
        center_x: root.width / 4
        on_press: root.clear_widgets()

    Button:
        text: 'Game 2'
        font_size: '32'
        background_color: 0, 0 ,0, 0
        size_hint_y: None
        height: '48dp'
        top: root.top / 1.66
        center_x: root.width / 4
        on_release: dropdown.select('C')

<Game1>:
    Button:
        id: tst
        text: 'Test'
        font_size: '32'
        background_color: 0, 0 ,0, 0
        size_hint_y: None
        height: '48dp'
        top: root.top
        center_x: root.width / 4

当用户单击游戏1时,我希望它跳到游戏1类,但是不能,因为该程序尚不知道它是否存在。 我不确定如何解决此问题。

您在谈论屏幕,但似乎并未使用实际的kivy屏幕。 您需要具有一个ScreenManager ,然后创建单独的Screen对象,然后可以将其放在小部件上。

从字面上看,这是当您搜索“奇异屏幕”时的第一个结果,请下次执行作业。

这是有关如何将现有代码转换为正确使用Screens的内容的快速教程。 考虑您具有以下.kv代码:

<First@Button>:
    text: "I'm first"

<Second@Button>:
    text: "I'm second..."

您希望第一个按钮在一个屏幕上,第二个按钮在另一个屏幕上。 首先,将它们放入Screen 通常,您只需将根窗口小部件更改为屏幕,然后将所有其他窗口小部件移至屏幕中。 现在,示例如下所示:

<First@Screen>:
    name: "first_screen" # a name is like an id but only for the screen manager
    Button:
        text: "I'm first"

<Second@Screen>:
    name: "second_screen"
    Button:
        text: "I'm second..."

您现在需要一个屏幕管理器。 在其中,添加要使用的所有屏幕。 我将其设为根小部件。 我还将这样做,以便在第一个屏幕上按下按钮会切换到第二个屏幕,反之亦然:

ScreenManager:
    First:
    Second:

<First@Screen>:
    name: "first_screen"
    Button:
        text: "I'm first"
        # root.manager.current is magic that gives you the current screen manager
        on_release: root.manager.current = "second_screen"

<Second@Screen>:
    name: "second_screen"
    Button:
        on_release: root.manager.current = "first_screen"
        text: "I'm second..."

而已! 您可以更改过渡类型和其他详细信息,请参阅docs

要查看此示例,请运行以下.py文件:

from kivy.base import runTouchApp
from kivy.lang import Builder

runTouchApp(Builder.load_string("""
ScreenManager:
    First:
    Second:

<First@Screen>:
    name: "first_screen" # a name is like an id but only for the screen manager
    Button:
        text: "I'm first"
        # root.manager.current is magic that gives you the current screen manager
        on_release: root.manager.current = "second_screen"

<Second@Screen>:
    name: "second_screen"
    Button:
        on_release: root.manager.current = "first_screen"
        text: "I'm second..."   
            """))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM