注意:我不是程序员,也不知道我应该使用的确切术语。这是一个基本的程序,我并不试图使用文字“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级但是它不能,因为程序不知道它还存在。我不知道如何去解决这个问题。
我并不想用文字屏幕。我只需要制作一个基本程序,并希望尽可能简单。我不是一位经验丰富的程序员,所以我不知道我应该使用的确切术语。 –
别担心,这很简单。术语很简单:有一个ScreenManager小部件只能包含一个屏幕。然后是屏幕可以包含你已经拥有的任何小部件。看看一些[示例](https://kivy.org/docs/api-kivy.uix.screenmanager.html#basic-usage)。你不需要改变任何代码,只需要大约10-15行。卡住时回复。 –
谢谢。我想我会试一试。也许这会帮助我让我的下一个学期更先进。 –