简体   繁体   English

Kivy:如何修复不显示 GUI 的屏幕

[英]Kivy: How to fix screens not displaying GUI

I'm very new to Kivy and have been constantly running into issues and errors.我对 Kivy 很陌生,并且经常遇到问题和错误。 A common problem I have is running a python program, seeing that the .kv file is displaying a window, but there is nothing inside the window.我遇到的一个常见问题是运行 python 程序,看到 .kv 文件正在显示一个窗口,但窗口内没有任何内容。 The purpose of the code is to display a window starting on the LoginScreen and switching between a separate HomeScreen with a press of a button.代码的目的是显示一个从 LoginScreen 开始的窗口,并通过按下按钮在单独的 HomeScreen 之间切换。 Any solutions would be helpful.任何解决方案都会有所帮助。 Thanks.谢谢。

Python file:蟒蛇文件:

import os
from kivy.app import App
from kivy.config import Config
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '600')
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.stacklayout import StackLayout
from kivy.metrics import dp
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty, Clock
from kivy.graphics.vertex_instructions import Line, Rectangle, Ellipse
from kivy.graphics.context_instructions import Color

#transition_direction = StringProperty('down') <--- Ignore this

class HomeScreen(Screen):
    def returnBtn(self):
        sm.current = 'LoginScreen'

class LoginScreen(Screen):

    email = ObjectProperty(None)
    password = ObjectProperty(None)
    login_lst = []
    passwordStudents = open('passwords/passwordStudents.txt').read()
    passwordTeachers = open('passwords/passwordTeachers.txt').read()
    passwordAdmin = open('passwords/passwordAdmin.txt').read()

    def loginBtn(self):
        self.login_lst.append(self.email.text)
        self.login_lst.append(self.password.text)
        if self.login_lst[0] and self.login_lst[-1] in self.passwordStudents:
            print('Student: True')
            print('Teacher: False')
            print('Admin: False')
            self.reset()
            self.home()
        elif self.login_lst[0] and self.login_lst[-1] in self.passwordTeachers:
            print('Student: False')
            print('Teacher: True')
            print('Admin: False')
            self.reset()
            self.home()
        elif self.login_lst[0] and self.login_lst[-1] in self.passwordAdmin:
            print('Student: False')
            print('Teacher: False')
            print('Admin: True')
            self.home()
            self.reset()
        else:
            print('Student: False')
            print('Teacher: False')
            print('Admin: False')
            self.reset()

    def reset(self):
        self.email.text = ''
        self.password.text = ''

    def home(self):
        #transition_direction = 'down' <--- Ignore this
        sm.current = 'HomeScreen'

class WindowManager(ScreenManager):
    pass

sm = WindowManager()

screens = [LoginScreen(name='login'), HomeScreen(name='home')]
for screen in screens:
    sm.add_widget(screen)

sm.current = 'login'

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

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

Kivy file:基维文件:

    <LoginScreen>:
    name: "login"
    email: email
    password: psswrd
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "Login"
            pos: 0, root.height/3
            font_size: 100
        GridLayout:
            cols: 2
            Label:
                size_hint: None, None
                width: "175dp"
                height: "50dp"
                text: "Email:"
                font_size: 50
            TextInput:
                id: email
                multiline: False
                size_hint: None, None
                width: "200dp"
                height: "50dp"
                font_size: (root.width**2 + root.height**2) / 14**4
        GridLayout:
            cols: 2
            Label:
                size_hint: None, None
                width: "175dp"
                height: "50dp"
                text: "Password:"
                font_size: 50
            TextInput:
                id: psswrd
                multiline: False
                size_hint: None, None
                width: "200dp"
                height: "50dp"
                password: True
                font_size: (root.width**2 + root.height**2) / 14**4
        FloatLayout:
            Button:
                size_hint: None, None
                width: "150dp"
                height: "100dp"
                pos: root.width/2 - 150, root.height/4 - 150
                text: "Submit"
                font_size: 60
                on_release:
                    root.loginBtn()

<HomeScreen>:
    name: "home"
    Button:
        text: "Go back"
        on_release:
            root.returnBtn()

Edit: I have noticed that the code runs when I comment out these three lines.编辑:我注意到当我注释掉这三行代码时代码会运行。 Why is that?这是为什么?

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

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

You are building your screens before your kv file is loaded, so the LoginScreen and the HomeScreen have nothing in them.您在加载kv文件之前构建screens ,因此LoginScreenHomeScreen中没有任何内容。 If your kv file is correctly named ( my.kv ) then it will be automatically loaded when MyApp().run() is run and you can build your screens inside the build() method.如果您的kv文件被正确命名( my.kv ),那么它将在MyApp().run()运行时自动加载,您可以在build()方法中构建screens If your kv file is not named that way, you can just add a call to Builder.load_file() in your build() method before your create the screens .如果您的kv文件没有以这种方式命名,您可以在创建screens之前在build()方法中添加对Builder.load_file()的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM