【发布时间】:2018-02-15 13:05:28
【问题描述】:
我不太熟悉 screenmanager 类的用法,因此尽管我将当前屏幕设置为“Title 4”,但我无法切换到名为“Title 4”的屏幕。由于某些奇怪的原因(“Title 2”),当前屏幕保持不变。我正在尝试使用 AnotherScreen(Screen) 类中的方法 ok_button_hit() 切换屏幕。有人能指出我在这里犯的错误吗?
ABC.py 文件:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.listview import ListItemButton
check_id=0
menu=''
class FoodListButton(ListItemButton):
pass
class ScreenManagement(ScreenManager):
pass
class LoginScreen(Screen):
username_input = ObjectProperty()
passwd_input = ObjectProperty()
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.boxp = BoxLayout(orientation='vertical')
self.labs = Label(text='')
self.boxp.add_widget(self.labs)
self.box = BoxLayout()
self.box.orientation = 'horizontal'
btn5 = Button(text="ok", size_hint=(0.5, 0.5))
btn5.bind(on_release=self.success)
self.box.add_widget(btn5)
self.box.add_widget(Button(text='cancel', size_hint=(0.5, 0.5), on_release=self.cancel_button_hit))
self.boxp.add_widget(self.box)
self.popup1 = Popup(title="LOGIN SUCCESSFUL",
content=self.boxp,
size=(200, 200),
size_hint=(0.3, 0.3),
auto_dismiss=True)
def checkin(self):
if self.username_input.text == "root" and self.passwd_input.text == "root":
self.labs.text = self.username_input.text
self.popup1.open()
else:
self.popup1.title = 'Failed Login'
self.labs.text = 'Failed'
self.popup1.open()
def cancel_button_hit(self, instance):
self.popup1.dismiss()
def success(self, instance):
self.popup1.dismiss()
self.manager.current = 'Title 2'
class AnotherScreen(Screen):
def __init__(self, **kwargs):
super(AnotherScreen, self).__init__(**kwargs)
self.boxp = BoxLayout(orientation='vertical')
self.labs = TextInput(multiline=False)
self.boxp.add_widget(self.labs)
self.box = BoxLayout()
self.box.orientation = 'horizontal'
btn5 = Button(text="OK", size_hint=(0.5, 0.5), on_release=self.ok_button_hit)
self.box.add_widget(btn5)
self.box.add_widget(Button(text='cancel', size_hint=(0.5, 0.5), on_release=self.cancel_button_hit))
self.boxp.add_widget(self.box)
self.popup1 = Popup(title="Enter Customer ID",
content=self.boxp,
size=(400, 400),
size_hint=(0.3, 0.3),
auto_dismiss=False)
self.popup2 = Popup(title="Failed",
content=Label(text='''You don't seem to have a customer id.
Please check in first'''),
size=(400, 400),
size_hint=(0.3, 0.3),
auto_dismiss=True)
def check_cust_id(self):
self.custid = self.labs.text
#check with database
if True:
return True
else:
return False
def cancel_button_hit(self, instance):
self.popup1.dismiss()
def ok_button_hit(self, instance):
if self.check_cust_id():
self.popup1.dismiss()
global menu
k='Title'+menu
print(k)
self.manager.current = k
else:
self.popup1.dismiss()
self.popup2.open()
self.popup1.dismiss()
def res_clicked(self):
global menu
menu = ' 4'
self.popup1.open()
def lan_clicked(self):
self.popup1.open()
global menu
menu = ' 5'
def game_clicked(self):
self.popup1.open()
global menu
menu += ' 6'
def checkout_clicked(self):
self.popup1.open()
global menu
menu += ' 7'
def check_in(self):
self.manager.current = 'Title 3'
class Res_Page(Screen):
total_food_bill = 0
food_input = ObjectProperty()
food_list = ObjectProperty()
def __init__(self, **kwargs):
super(Res_Page, self).__init__(**kwargs)
self.boxp = BoxLayout(orientation='vertical')
self.labs = Label(text=" ")
self.boxp.add_widget(self.labs)
self.box = BoxLayout()
self.box.orientation = 'horizontal'
btn5 = Button(text="OK", size_hint=(0.5, 0.5), on_release=self.ok_button_hit)
self.box.add_widget(btn5)
self.popup1 = Popup(title="Food Bill",
content=self.boxp,
size=(200, 200),
size_hint=(0.3, 0.3),
auto_dismiss=False)
self.popup2 = Popup(title="Error",
content=Label(text='''Sorry we don't make that'''),
size=(200, 200),
size_hint=(0.3, 0.3),
auto_dismiss=True)
def ok_button_hit(self, instance):
self.popup1.dismiss()
self.manager.current = 'Title 2'
def submit_food(self):
food_name = self.food_input.text
# Check if food is present
if (True):
self.food_list.adapter.data.extend([food_name])
self.food_list._trigger_reset_populate()
# Retreive price and subtract from total amount
else:
self.popup2.open()
def delete_food(self, *args):
if self.food_list.adapter.selection:
selection = self.food_list.adapter.selection[0].text
# Check if food is present
if (True):
self.food_list.adapter.data.remove(selection)
self.student_list._trigger_reset_populate()
# Retreive price and subtract from total amount
else:
self.popup2.open()
def bill_food(self):
self.labs.text = str(self.total_food_bill)
self.popup1.open()
class checkinscrn(Screen):
def checkin_nextscr(self):
self.manager.current = 'Title 4'
class checkinadv(Screen):
pass
class MainApp(App):
def build(self):
self.root = Builder.load_file('ABC.kv')
my_screenmanager = ScreenManager()
screen1 = LoginScreen(name='Title 1')
screen2 = AnotherScreen(name='Title 2')
screen3 = checkinscrn(name='Title 3')
screen4 = AnotherScreen(name='Title 4')
screen14 = checkinadv(name='Title 14')
my_screenmanager.add_widget(screen1)
my_screenmanager.add_widget(screen2)
my_screenmanager.add_widget(screen3)
my_screenmanager.add_widget(screen4)
#my_screenmanager.add_widget(screen14)
return my_screenmanager
MainApp().run()
ABC.kv 文件:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
<ScreenManagement>:
transition: FadeTransition()
LoginScreen:
AnotherScreen:
Res_Page:
checkinscrn:
checkinadv:
<LoginScreen>:
name: 'Title 1'
orientation: "vertical"
username_input: username
passwd_input: passwd
padding: 10
spacing: 10
BoxLayout:
padding:[10,50]
spacing: 40
orientation: "vertical"
Label:
text: "Username"
size_hint_x: 0.4
size_hint_y: 0
size: [200, 50]
font_size: 20
pos_hint: {'left_x': 1, 'center_y': 0.5}
TextInput:
id: username
size_hint_x: 0.4
size_hint_y: 0
size: [400, 75]
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Label:
text: "Password"
size_hint_x: 0.4
size_hint_y: 0
size: [200, 50]
font_size: 20
pos_hint: {'left_x': 0, 'center_y': 0.5}
TextInput:
id: passwd
password: True
size_hint_x: 0.4
size_hint_y: 0
size: [400, 75]
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
on_release: root.checkin()
text: 'Login'
font_size: 50
size_hint_x: 0.4
size_hint_y: 0
size: [400, 75]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
<AnotherScreen>:
name: 'Title 2'
orientation: "vertical"
padding: 10
spacing: 10
BoxLayout:
padding:[10,50]
spacing: 40
orientation: "vertical"
Button:
on_release: root.check_in()
text: 'Check In'
font_size: 50
size_hint_x: 0.4
size_hint_y: 0
size: [400, 45]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
on_release: root.res_clicked()
text: 'Restaurant'
font_size: 50
size_hint_x: 0.4
size_hint_y: 0
size: [400, 45]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
on_release: root.lan_clicked()
text: 'Laundry'
font_size: 50
size_hint_x: 0.4
size_hint_y: 0
size: [400, 45]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
on_release: root.game_clicked()
text: 'Gaming'
font_size: 50
size_hint_x: 0.4
size_hint_y: 0
size: [400, 45]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
on_release: root.checkout_clicked()
text: 'Check Out'
font_size: 50
size_hint_x: 0.4
size_hint_y: 0
size: [400, 45]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
on_release: app.root.current = 'Title 1'
text: 'Log Out'
font_size: 50
size_hint_x: 0.4
size_hint_y: 0
size: [400, 45]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
<Res_Page>:
name: 'Title 4'
orientation: "vertical"
food_input: food_name
food_list: food_list_view
padding: 10
spacing: 10
BoxLayout:
size_hint_y: None
height: "40dp"
Label:
text: "Dish"
TextInput:
id: food_name
BoxLayout:
size_hint_y: None
height: "40dp"
Button:
text: "Submit"
size_hint_x: 15
on_press: root.submit_food()
Button:
text: "Delete"
size_hint_x: 15
on_press: root.delete_food()
Button:
text: "Generate Bill"
size_hint_x: 15
on_press: root.bill_food()
ListView:
id: students_list_view
adapter:
ListAdapter(data=[], cls=main.FoodListButton)
<checkinscrn>:
name: 'Title 3'
orientation: "vertical"
padding: 10
spacing: 10
name_input: name
address_input: addr
checkin_input: cin
checkout_input: cout
BoxLayout:
padding:[10,50]
spacing: 40
orientation: "vertical"
Label:
text: 'ENTER NAME'
size_hint_x: 0.4
size_hint_y: 0
size: [50, 25]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
TextInput:
id: name
size_hint_x: 0.5
size_hint_y: 0
size: [50, 25]
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Label:
text: 'ENTER ADDRESS'
size_hint_x: 0.5
size_hint_y: 0
size: [50, 25]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
TextInput:
id: addr
size_hint_x: 0.5
size_hint_y: 0
size: [50, 25]
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Label:
text: 'ENTER CHECK IN DATE'
size_hint_x: 0.5
size_hint_y: 0
size: [50, 25]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
TextInput:
id: cin
size_hint_x: 0.5
size_hint_y: 0
size: [50, 25]
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Label:
text: 'ENTER CHECK OUT DATE'
size_hint_x: 0.5
size_hint_y: 0
size: [50, 25]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
TextInput:
id: cout
size_hint_x: 0.5
size_hint_y: 0
size: [50, 25]
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
on_release: root.checkin_nextscr()
text: 'CHECK IN'
font_size: 50
size_hint_x: 0.4
size_hint_y: 0
size: [50, 25]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
<checkinadv>:
name: 'Title 14'
orientation: "vertical"
padding: 10
spacing: 10
BoxLayout:
padding:[10,50]
spacing: 40
orientation: "vertical"
Label:
text: 'your check in id is'
size_hint_x: 0.4
size_hint_y: 0
size: [50, 25]
font_size: 20
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
【问题讨论】:
-
似乎对我有用。请描述您在运行应用程序时采取的确切步骤以及出现的问题。
-
该应用程序基本上是一个酒店管理系统,用户必须先登录才能进入主菜单屏幕。问题在于主菜单中的餐厅按钮。当点击它时,当前屏幕应该被餐厅屏幕(在本例中为标题 4)替换,但这似乎没有发生,当前屏幕保持不变。
标签: python python-3.x kivy