2017-05-26 97 views
0

注意:我不是程序员,也不知道我应该使用的确切术语。这是一个基本的程序,我并不试图使用文字“kivy屏幕”或kivy屏幕管理器。Python - 如何为我的kivy应用程序添加第二个“屏幕”?

我已经花了几个小时搜索谷歌试图解决这个问题,我来了很短。我有一个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级但是它不能,因为程序不知道它还存在。我不知道如何去解决这个问题。

回答

1

你说的是屏幕,但似乎没有使用实际的kivy屏幕。您需要有一个ScreenManager,然后创建可以放置小部件的单独Screen对象。

这实际上是谷歌“kivy屏幕”的第一个结果,请下次做功课。

下面是如何将现有代码转换为正确使用屏幕的快速教程。想想你有以下.kv代码:

<[email protected]>: 
    text: "I'm first" 

<[email protected]>: 
    text: "I'm second..." 

你想第一个按钮是一个屏幕,并在另一个第二个上。首先,让他们成为Screen。通常,您只需将根窗口小部件更改为屏幕,然后将所有其他窗口小部件移至屏幕中。现在的示例如下所示:

<[email protected]>: 
    name: "first_screen" # a name is like an id but only for the screen manager 
    Button: 
     text: "I'm first" 

<[email protected]>: 
    name: "second_screen" 
    Button: 
     text: "I'm second..." 

您现在需要一个屏幕管理器。在其中添加您要使用的所有屏幕。我将使它成为根部件。我还会让这个按第一画面切换到第二个屏幕,反之亦然按钮:

ScreenManager: 
    First: 
    Second: 

<[email protected]>: 
    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" 

<[email protected]>: 
    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: 

<[email protected]>: 
    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" 

<[email protected]>: 
    name: "second_screen" 
    Button: 
     on_release: root.manager.current = "first_screen" 
     text: "I'm second..." 
      """)) 
+0

我并不想用文字屏幕。我只需要制作一个基本程序,并希望尽可能简单。我不是一位经验丰富的程序员,所以我不知道我应该使用的确切术语。 –

+0

别担心,这很简单。术语很简单:有一个ScreenManager小部件只能包含一个屏幕。然后是屏幕可以包含你已经拥有的任何小部件。看看一些[示例](https://kivy.org/docs/api-kivy.uix.screenmanager.html#basic-usage)。你不需要改变任何代码,只需要大约10-15行。卡住时回复。 –

+0

谢谢。我想我会试一试。也许这会帮助我让我的下一个学期更先进。 –

相关问题