【发布时间】:2017-10-27 17:38:06
【问题描述】:
注意:我不是程序员,也不知道我应该使用的确切术语。这是一个基本程序,我不想使用文字“kivy 屏幕”或 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 类,但它不能,因为程序还不知道它存在。我不确定如何解决这个问题。
【问题讨论】: