我只是想設置我的菜單。我看起來很好,然後我發現了ScreenManager,並決定使用它是一件好事。所以我將我的代碼移植到一個新文件中來試用它,但是我的屏幕布局完全沒有了。Kivy 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
我沒有給大小我仍然使其基於根改變的值,並且我不知道什麼是影響它。
也許你可以附上截圖或兩個顯示什麼是錯的? – jligeza
我添加了一些屏幕截圖 – Arkyris