我只是想设置我的菜单。我看起来很好,然后我发现了ScreenManager并决定使用它是一件好事。所以我将我的代码移植到一个新文件中进行试用,但我的屏幕位置完全不合适。
我想要的是什么:
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.tabbedpanel import TabbedPanelHeader
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
class MainMenu(Widget):
pass
class G_A_M_E_App(App):
def build(self):
return MainMenu()
if __name__=='__main__':
G_A_M_E_App().run()
及其.kv文件
#:kivy 1.9.1
<MainMenu>:
FloatLayout:
width: root.width * 1/3
height: root.height * 1/2
center_x: root.width * 1/2
center_y: root.height * 1/2
canvas:
Color:
rgba: 0,0,1,.5
Rectangle:
size: self.size
pos: self.pos
Button:
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .90}
text: 'Play'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
Button:
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .70}
text: 'Load'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
Button:
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .50}
text: 'New Game'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
Button:
id: help
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .30}
text: 'Help'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
on_press: root.helpm()
Button:
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .10}
text: 'Quit'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
1 而且这是一个看起来不那样的人
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
class StartMenu(Screen):
pass
class NewGame(Screen):
pass
class LoadGame(Screen):
pass
class ScreenManager(ScreenManager):
pass
sm = Builder.load_file("main.kv")
class G_A_M_E_App(App):
def build(self):
return sm
if __name__=='main__':
G_A_M_E_App().run()
及其.kv文件
ScreenManager:
StartMenu:
NewGame:
LoadGame:
<StartMenu>:
name: 'start_menu'
FloatLayout:
width: root.width * 1/3
height: root.height * 1/2
center_x: root.width * 1/2
center_y: root.height * 1/4
canvas:
Color:
rgba: 0,0,1,.5
Rectangle:
size: root.size
pos: root.pos
Button:
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .90}
text: 'Play'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
Button:
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .70}
text: 'Load'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
Button:
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .50}
text: 'New Game'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
Button:
id: help
size_hint: (.5, .10)
pos_hint: {'center_x': .5, 'center_y' : .30}
text: 'Help'
color: [1, 0, 0, 1]
font_size: self.height * 5/6
border: [25, 25, 25, 25]
<NewGame>:
name: 'new_game'
Button:
on_release: app.root.current = 'new'
text: 'New Game'
font_size: 50
<LoadGame>:
name: 'load_game'
Button:
on_release: app.root.current = 'load'
text: 'Load'
font_size: 50
我没有更改任何大小的值,我仍然以root为基础,我不确定是什么影响它。
答案 0 :(得分:2)
屏幕管理器是相对布局。这是正在发生的事情。 https://kivy.org/docs/api-kivy.uix.relativelayout.html#kivy-uix-relativelayout-common-pitfalls
答案 1 :(得分:1)
尝试将按钮放在BoxLayout中。像这样:
FloatLayout:
canvas:
Color:
rgba: 0,0,1,.5
Rectangle:
size: root.size
pos: root.pos
BoxLayout:
spacing: 20
orientation: 'vertical'
size_hint: (.5, .5)
pos_hint: {'center_x':.5, 'center_y':.5}
Button:
Button: