【发布时间】:2019-03-09 17:20:00
【问题描述】:
我是 Kivy 的新手,但能够创建单屏应用。在我需要添加更多屏幕之前,一切看起来都很好。一页应用程序显示正确,但是当我添加屏幕时,BoxLayouts 不再按预期工作。小部件全部呈现在屏幕底部。我的问题是我在实施 ScreenManager 时做错了什么?
单屏应用如下所示:
我决定让它多屏,但是当我添加 ScreenManager 逻辑时,生成的屏幕如下所示:
我从this tutorial获取信息
我的 main.py 文件如下所示:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class Pay_screen(Screen):
pass
class Survey_screen(Screen):
pass
class Finish_screen(Screen):
pass
class Sm(ScreenManager):
pass
class Next_root(BoxLayout):
pass
class Nq_disagree_label(Label):
pass
class Nq_question_label(Label):
pass
class Nq_button(Button):
pass
class survey_form(BoxLayout):
#dept_button = ObjectProperty()
def send_survey(self):
mypopup = MyPopup()
mypopup.show_popup('Survey', 'Survey sent!', 'OK!')
def add_comment(self):
mypopup = MyPopup()
mypopup.show_popup('Comment', 'This is where a comment is added.', 'OK!')
def close_app(self):
App.get_running_app().stop()
class MyPopup(Popup):
def show_popup(self, title_text, label_text, button_text):
mytext= label_text
content = BoxLayout(orientation="vertical")
content.add_widget(Label(text=mytext, font_size=20, text_size=(300, None)))
mybutton = Button(text="Ok!", size_hint=(1,.20), font_size=20)
content.add_widget(mybutton)
mypopup = Popup(content = content,
title = title_text,
auto_dismiss = False,
size_hint = (.5, .5)) #,
#font_size = 20)
mybutton.bind(on_press=mypopup.dismiss)
mypopup.open()
class nextqualApp(App):
icon = 'nextqual.png'
title = 'Pay / survey / join'
if __name__ == '__main__':
nextqualApp().run()
我的屏幕管理器的 KV 文件如下所示:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
Sm:
transition: FadeTransition()
Survey_screen:
Pay_screen:
<nq_button@Button>:
halign: "center"
text: "Add\ncomment"
size_hint_x: 10
<Nq_disagree_label@Label>:
halign: "left"
size_hint_x: 10
text:"Strongly\ndisagree"
<Nq_agree_label@Label>:
halign: "left"
size_hint_x: 10
text:"Strongly\nagree"
<Nq_question_label@Label>:
halign: "left"
font_size: "24"
size_hint_x: 25
<Pay_screen>:
name: 'pay'
#orientation: "vertical"
#padding: 6
#font_size: 24
BoxLayout:
height: "40dp"
Button:
text: "Pay your bill"
on_release: app.root.current = 'pay'
Button:
text: "Tell us how we did"
on_release: app.root.current = 'survey'
Button:
text: "I'm finished"
BoxLayout:
Button:
text: "Pay your bill"
on_release: app.root.current = 'survey'
<Survey_screen>:
name: "survey"
#orientation: "vertical"
#padding: 6
#font_size: "24"
BoxLayout:
height: "40dp"
Button:
text: "Pay your bill"
on_release: app.root.current = 'pay'
Button:
text: "Tell us how we did"
on_release: app.root.current = 'survey'
Button:
text: "I'm finished"
BoxLayout:
halign: "center"
Image:
source: "logo.jpg"
#size: self.size
BoxLayout:
height: "80dp"
size_hint_y: None
Label:
bold: True
#color: 10,10,10,10
#halign: "center"
#text_size: self.size
markup: True
text: "[color=f9f752]Description[/color]"
multiline: True
#size_hint_x: 25
text_size: self.size
halign: 'center'
valign: 'middle'
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The service I recieved from start to finish was excellent."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "I waited an appropriate amount of time for my food and drink."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The quality and presentation of the food was outstanding."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The prices provide good value for money."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The atmosphere was relaxing and enjoyable."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The overall cleanliness was very acceptable."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "I will recommend Synterest to my friends and family."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "100dp"
size_hint_y: None
Label:
size_hint_x: 40
Button:
size_hint_x: 20
text:"Send survey"
halign: "center"
on_press: root.send_survey()
Label:
size_hint_x: 40
没有屏幕管理器的KV文件是这样的:
nq_button@Button>:
halign: "center"
text: "Add\ncomment"
size_hint_x: 10
<Nq_disagree_label@Label>:
halign: "left"
size_hint_x: 10
text:"Strongly\ndisagree"
<Nq_agree_label@Label>:
halign: "left"
size_hint_x: 10
text:"Strongly\nagree"
<Nq_question_label@Label>:
halign: "left"
font_size: "24"
size_hint_x: 25
survey_form:
<survey_form>:
name: "survey"
orientation: "vertical"
padding: 6
font_size: "24"
BoxLayout:
height: "40dp"
Button:
text: "Pay your bill"
on_release: app.root.current = 'pay'
Button:
text: "Tell us how we did"
on_release: app.root.current = 'survey'
Button:
text: "I'm finished"
BoxLayout:
halign: "center"
Image:
source: "logo.jpg"
#size: self.size
BoxLayout:
height: "80dp"
size_hint_y: None
Label:
bold: True
#color: 10,10,10,10
#halign: "center"
#text_size: self.size
markup: True
text: "[color=f9f752]At Synterest, we care deeply about your impressions of our food, service and atmosphere. We would be very grateful if you took a few moments to give us your feedback on how you enjoyed your meal.[/color]"
multiline: True
#size_hint_x: 25
text_size: self.size
halign: 'center'
valign: 'middle'
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The service I recieved from start to finish was excellent."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "I waited an appropriate amount of time for my food and drink."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The quality and presentation of the food was outstanding."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The prices provide good value for money."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The atmosphere was relaxing and enjoyable."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "The overall cleanliness was very acceptable."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_question_label:
text: "I will recommend Synterest to my friends and family."
BoxLayout:
height: "40dp"
size_hint_y: None
Nq_disagree_label:
Slider:
size_hint_x: 80
value: 50
Nq_agree_label:
nq_button:
on_press: root.add_comment()
BoxLayout:
height: "100dp"
size_hint_y: None
Label:
size_hint_x: 40
Button:
size_hint_x: 20
text:"Send survey"
halign: "center"
on_press: root.send_survey()
Label:
size_hint_x: 40
【问题讨论】:
标签: python kivy screen kivy-language