我正在用Kivy,Python编写一个小游戏,并且创建了一个下拉菜单,对于kivy来说我是一个非常新的人,我不确定每次加载菜单时如何运行下拉菜单代码新屏幕,因此无论我在哪个屏幕上,我都可以选择该菜单。
我已经在Google各处寻找了答案,但找不到任何东西!
Main.py文件!
import kivy
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.core.image import Image as CoreImage
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.uix.dropdown import DropDown
from kivy.config import Config
所有课程都在这里
Window.clearcolor = (0.50, 0.50, 0.50, 0.50)
class CustomDropDown(BoxLayout):
pass
class RootScreen(ScreenManager):
pass
class MyScreenManager(ScreenManager):
pass
class Game(Screen):
def on_pre_enter(self):
Window.size = (800, 600)
Window.clearcolor = (0.50, 0.50, 0.50, 0.50)
class Game2(Screen):
def on_pre_enter(self):
Window.size = (800, 600)
Window.clearcolor = (0.50, 0.50, 0.50, 0.50)
class Game3(Screen):
def on_pre_enter(self):
Window.size = (800, 600)
Window.clearcolor = (0.50, 0.50, 0.50, 0.50)
class Game4(Screen):
def on_pre_enter(self):
Window.size = (800, 600)
Window.clearcolor = (0.50, 0.50, 0.50, 0.50)
class Game5(Screen):
def on_pre_enter(self):
Window.size = (800, 600)
Window.clearcolor = (0.50, 0.50, 0.50, 0.50)
class Title(Screen):
def on_pre_enter(self):
Window.size = (800, 600)
def closewindow(self):
Window.close()
class MyWindow(App):
def build(self):
self.title = "TheAvengersGame"
return MyScreenManager()
return RootWidget()
return CustomDropDown()
window = MyWindow()
window.run()
mywindow.kv文件!
<MyScreenManager>:
Title:
Game:
Game2:
Game3:
Game4:
Game5:
所有.kv屏幕
<Title>:
name: "Title"
FloatLayout:
Label:
text: "Avengers Assemble"
pos_hint: {"x": .3, "y": .7}
size_hint: .4, .2
font_size: "50dp"
Button:
id: PlayButton
text: "Play!"
pos_hint: {"x": .3, "y": .4}
size_hint: .4, .1
on_press:
root.manager.current = "main"
Button:
id: QuitButton
text: "Quit!"
pos_hint: {"x": .3, "y": .2}
size_hint: .4, .1
on_press:
root.closewindow()
<Game2>:
name: "Avengers Tower"
FloatLayout:
Label:
text: "You are in the Avengers Tower!"
pos_hint: {"x": .45, "y": .6}
font_size: "20dp"
size_hint: None, None
Button:
text: "North"
pos_hint: {"x": .45, "y": .85}
size_hint: None, None
height: 50
on_press:
root.manager.current = "AvengersCourtyard"
<Game3>:
name: "AvengersCourtyard"
FloatLayout:
Label:
text: "You are in the Avengers Courtyard!"
font_size: "20dp"
pos_hint: {"x": .45, "y": .6}
size_hint: None, None
Button:
text: "North"
pos_hint: {"x": .45, "y": .85}
height: 50
size_hint: None, None
on_press:
root.manager.current = "ChinaTown"
Button:
text: "East"
pos_hint: {"x": .8, "y": .4}
size_hint: None, None
height: 50
on_press:
root.manager.current = "Apartments"
Button:
text: "South"
pos_hint: {"x": .45, "y": .05}
size_hint: None, None
height: 50
on_press:
root.manager.current = "Avengers Tower"
<Game4>:
name: "Apartments"
FloatLayout:
Label:
text: "You are in the Apartments!"
pos_hint: {"x": .45, "y": .6}
font_size: "20dp"
size_hint: None, None
Button:
text: "West"
pos_hint: {"x": .05, "y": .45}
size_hint: None, None
height: 50
on_press:
root.manager.current = "AvengersCourtyard"
<Game5>:
name: "ChinaTown"
FloatLayout:
Label:
text: "You are in China Town!"
pos_hint: {"x": .45, "y": .6}
font_size: "20dp"
size_hint: None, None
Button:
text: "South"
pos_hint: {"x": .45, "y": .05}
size_hint: None, None
height: 50
on_press:
root.manager.current = "AvengersCourtyard"
<Game>:
name: "main"
FloatLayout:
Button:
text: "<- Back"
font_size: "15dp"
pos: 40, 510
size_hint: None, None
height: 50
on_press:
root.manager.current = "Title"
Label:
text: "Choose a character!"
font_size: "50dp"
pos_hint: {"x": .3, "y": .7}
size_hint: .4, .2
ToggleButton:
text: "Iron Man"
font_size: "15dp"
pos: 200, 150
size_hint: None, None
height: 80
group: "character"
on_press:
char = "Iron Man"
ToggleButton:
text: "Thor"
font_size: "15dp"
pos: 350, 150
size_hint: None, None
height: 80
group: "character"
on_press:
char = "Thor"
ToggleButton:
text: "Scarlett Witch"
font_size: "15dp"
pos: 500, 150
size_hint: None, None
height: 80
group: "character"
on_press:
char = "Scarlett Witch"
Button:
text: "Play!"
height: 50
font_size: "15dp"
pos: 650, 60
size_hint: None, None
on_press:
root.manager.current = "Avengers Tower"
<CustomDropDown>:
Button:
id: btn
text: 'Press'
on_release: dropdown.open(self)
on_parent: dropdown.dismiss()
size_hint_y: None
height: '48dp'
DropDown:
id: dropdown
on_select: btn.text = '{}'.format(args[1])
Button:
text: 'First Item'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('First Item')
Button:
text: 'Second Item'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('Second item')
Button:
text: 'Third Item'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('Third Item')
根本不会创建下拉菜单。 我知道了,因为我没有要求将其放在屏幕上。