Я пишу небольшую игру на 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')
Раскрывающееся меню не создается вообще.Я знаю это, поскольку я не призывал его поставить на экран.