Python 面对kivy中切换屏幕的问题

Python 面对kivy中切换屏幕的问题,python,python-3.x,kivy,Python,Python 3.x,Kivy,我不太熟悉screenmanager类的用法,因此我无法切换到名为“Title 4”的屏幕,尽管我将当前屏幕设置为“Title 4”。由于一些奇怪的原因(“Title 2”),当前屏幕保持不变。我尝试使用ok_button_hit()方法切换屏幕出现在另一个屏幕上。有人能指出我在这里犯的错误吗 ABC.py文件: #!/usr/bin/env python3 # -*- coding: utf-8 -*- from kivy.app import App from kivy.lang impor

我不太熟悉screenmanager类的用法,因此我无法切换到名为“Title 4”的屏幕,尽管我将当前屏幕设置为“Title 4”。由于一些奇怪的原因(“Title 2”),当前屏幕保持不变。我尝试使用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}
#/usr/bin/env蟒蛇3
#-*-编码:utf-8-*-
#:导入FadeTransition kivy.uix.screenmanager.FadeTransition
:
转换:FadeTransition()
登录筛选:
另一屏幕:
第页:
checkinscrn:
checkinadv:
:
名称:“标题1”
方向:“垂直”
用户名\输入:用户名
passwd\u输入:passwd
填充:10
间距:10
盒子布局:
填充:[10,50]
间距:40
方向:“垂直”
标签:
文本:“用户名”
大小提示:0.4
大小提示:0
尺寸:[200,50]
字体大小:20
位置提示:{'left_x':1,'center_y':0.5}
文本输入:
id:用户名
大小提示:0.4
大小提示:0
尺寸:[400,75]
位置提示:{'center_x':0.5,'center_y':0.5}
标签:
文本:“密码”
大小提示:0.4
大小提示:0
尺寸:[200,50]
字体大小:20
位置提示:{'left_x':0,'center_y':0.5}
文本输入:
id:passwd
密码:True
大小提示:0.4
大小提示:0
尺寸:[400,75]
位置提示:{'center_x':0.5,'center_y':0.5}
按钮:
发布时:root.checkin()
文本:“登录”
字体大小:50
大小提示:0.4
大小提示:0
尺寸:[400,75]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
:
名称:“标题2”
方向:“垂直”
填充:10
间距:10
盒子布局:
填充:[10,50]
间距:40
方向:“垂直”
按钮:
发布时:root.check\u-in()
文本:“签入”
字体大小:50
大小提示:0.4
大小提示:0
尺寸:[400,45]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
按钮:
发布时:root.res\u单击()
文字:“餐厅”
字体大小:50
大小提示:0.4
大小提示:0
尺寸:[400,45]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
按钮:
发布时:root.lan\u单击()
文字:“洗衣房”
字体大小:50
大小提示:0.4
大小提示:0
尺寸:[400,45]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
按钮:
发布时:root.game\u点击()
文字:“游戏”
字体大小:50
大小提示:0.4
大小提示:0
尺寸:[400,45]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
按钮:
发布时:root.checkout\u单击()
文本:“签出”
字体大小:50
大小提示:0.4
大小提示:0
尺寸:[400,45]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
按钮:
发布时:app.root.current='Title 1'
文本:“注销”
字体大小:50
大小提示:0.4
大小提示:0
尺寸:[400,45]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
:
名称:“标题4”
方向:“垂直”
食物输入:食物名称
食物列表:食物列表视图
填充:10
间距:10
盒子布局:
尺寸提示:无
高度:“40dp”
标签:
文字:“菜”
文本输入:
id:食物名称
盒子布局:
尺寸提示:无
高度:“40dp”
按钮:
案文:“提交”
尺寸提示:15
按:root.submit\u food()
按钮:
案文:“删除”
尺寸提示:15
按:root.delete\u food()
按钮:
文本:“生成账单”
尺寸提示:15
新闻:root.bill\u food()
列表视图:
id:学生列表视图
适配器:
ListAdapter(数据=[],cls=main.FoodListButton)
:
名称:“标题3”
方向:“垂直”
填充:10
间距:10
名称\输入:名称
地址\输入:地址
签入输入:cin
签出输入:cout
盒子布局:
填充:[10,50]
间距:40
方向:“垂直”
标签:
文本:“输入名称”
大小提示:0.4
大小提示:0
尺寸:[50,25]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
文本输入:
id:姓名
尺寸提示:0.5
大小提示:0
尺寸:[50,25]
位置提示:{'center_x':0.5,'center_y':0.5}
标签:
文本:“输入地址”
尺寸提示:0.5
大小提示:0
尺寸:[50,25]
字体大小:20
位置提示:{'center_x':0.5,'center_y':0.5}
文本输入:
地址:addr
尺寸提示:0.5
大小提示:0
尺寸:[50,25]
位置提示:{'center_x':0.5,'center_y':0.5}
标签:
文本:“输入签入日期”
尺寸提示:0.5
大小提示:0
尺寸:[50,25]
字体大小:20
位置提示:{'center_x':0.5,'cente