我的 sm.current(sm 是 WindowManager 的对象)似乎在 Kivy 中不起作用

Posted

技术标签:

【中文标题】我的 sm.current(sm 是 WindowManager 的对象)似乎在 Kivy 中不起作用【英文标题】:My sm.current (sm is object of WindowManager) doesn't appear to be working in Kivy 【发布时间】:2021-01-14 16:05:05 【问题描述】:

我正在制作一个基本的饮食应用程序,作为我高中项目的一部分。我正在尝试使用从 ScreenManager 继承的 WindowManager 类的对象在屏幕之间导航。但是在运行程序时,我只是得到一个空白屏幕。

代码:

import kivy
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
import mysql.connector as ms
from kivy.uix.popup import Popup
from kivy.uix.label import Label

db = ms.connect(user="root", password="jayanti1909", host="localhost", database="DIETIFY")
cursor = db.cursor()


class HomePage(Screen):

    def log_in_btn(self):
        sm.current = "LogIn"

    def sign_up_btn(self):
        sm.current = "SignUp"


class LogIn(Screen):

    def go_back_btn(self):
        sm.current = "HomePage"


class SignUp(Screen):
    name_of_person = ObjectProperty(None)
    height_of_person = ObjectProperty(None)
    weight = ObjectProperty(None)
    username = ObjectProperty(None)
    password = ObjectProperty(None)
    insert_query = ""
    record_tuple = ()

    def submit_btn(self):
        self.insert_query = "INSERT INTO UserDetails VALUES(%s,%s,%s,%s,%s)"
        if (self.name_of_person.text != ""
            and float(self.height_of_person.text) > 0
            and float(self.weight.text) > 0
            and self.username.text != ""
            and self.password.text != ""):
                while True:
                    try:
                        self.record_tuple = (self.name_of_person.text,
                                             float(self.height_of_person.text),
                                             float(self.weight.text),
                                             self.username.text,
                                             self.password.text)
                        cursor.execute(self.insert_query, self.record_tuple)
                        db.commit()
                        sm.current = "LogIn"
                        break
                    except:
                        invalid_sign_up()
                        self.username.text = ''
                        sm.current = "SignUp"
                self.reset()

    def go_back(self):
        sm.current = "HomePage"

    def reset(self):
        self.name.text = ""
        self.name_of_person.text = ""
        self.height_of_person.text = ""
        self.weight.text = ""
        self.username.text = ""
        self.password.text = ""


class WindowManager(ScreenManager):
    pass


def invalid_sign_up():
    pop = Popup(title='Invalid Login',
                content=Label(text='Invalid credentials'),
                size_hint=(None, None), size=(400, 400))
    pop.open()


kv = Builder.load_file("my.kv")
sm = WindowManager()

screens = [HomePage(name="HomePage"), LogIn(name="LogIn"), SignUp(name="SignUp")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "HomePage"


class MyApp(App):
    def build(self):
        return kv


if __name__ == '__main__':
    MyApp().run()

my.kv 文件:

<HomePage>:
name: "HomePage"
GridLayout:
    cols:1

    Label:
        text: "Welcome to Dietify"
        color : 1,0,0,0.7
        font_size : 60

    Button:
        font_size : 30
        id: btn_1
        height: 20
        background_color : 0.2, 0.8, 1, 0.6
        text: "Sign Up"
        on_release:
            root.sign_up_btn()
    Button:
        id: btn_2
        font_size : 30
        background_color: 0.2, 0.8, 1, 0.4
        text: "Login"
        on_release:
            root.log_in_btn()

<LogIn>:
    name: "LogIn"
    Button:
        text: "Go Back"
        on_release: root.go_back_btn()

<SignUp>:
    name : "SignUp"
    name_of_person : name_of_person
    height_of_person : height_of_person
    weight : weight
    username : username
    password : password
    GridLayout:
    cols : 1
    size : root.width, root.height

    GridLayout:
        cols : 2

        Label:
            background_color: 0,128,128,0.7
            text: "Name"
        TextInput:
            id : name_of_person
            multiline : False

        Label:
            background_color: 0,128,128,0.7
            text: "Height"
        TextInput:
            id : height_of_person
            multiline : "False"

        Label:
            background_color: 0,128,128,0.7
            text: "Weight"
        TextInput:
            id: weight
            multiline : "False"

        Label:
            background_color: 0,128,128,0.7
            text : "Username"
        TextInput:
            id: username
            multiline : False

        Label:
            background_color: 0,128,128,0.7
            text : "Password"
        TextInput:
            id: password
            multiline : False
    Button:
        id: btn
        text : "Submit"
        background_color :0.2, 0.8, 1, 0.6
        color : 0,0,0,1
        on_release: root.submit_btn()
    Button:
        text: "Go Back"
        on_release : root.go_back()

【问题讨论】:

【参考方案1】:

在您的 python 文件中,再次检查 MyApp 类... 应该是:

class MyApp(App):
    def build(self):
        return sm 

代替:

class MyApp(App):
    def build(self):
        return kv

由于应用程序正在返回 .kv 文件(它不能以任何方式呈现)而不是您的屏幕...

【讨论】:

以上是关于我的 sm.current(sm 是 WindowManager 的对象)似乎在 Kivy 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

JPA HSQL 不从扩展类中获取字段

数美(sm)面经

C语言SM4算法实现(基于GMSSL)

将打印媒体的引导网格从 col-xs 更改为 col-sm

CPU 超过 GPU

sm list-disks not found