You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy运行报错“未创建窗口,终止应用运行”求技术支援

Fixing "No window is created. Terminating application run" in Your Kivy App

Let's break down exactly why you're hitting this error and fix your app step by step. The root issues are small but critical mistakes in your code that prevent Kivy from properly initializing and displaying a window.

1. Incorrect ObjectProperty Setup

You defined your widget properties (entreprise, email, password) inside the __init__ method of CreateAccount—this won't work. Kivy's ObjectProperty needs to be declared at the class level (outside any method) so the KV language can bind to them. If they're tucked inside __init__, Kivy can't recognize them as valid widget references.

Fix it by moving the properties to the class scope:

class CreateAccount(Screen):
    # Declare properties at the class level
    entreprise = ObjectProperty(None)
    email = ObjectProperty(None)
    password = ObjectProperty(None)
    
    # You can remove the __init__ method entirely here—you don't need custom initialization

2. Broken submit Method & Undefined Variables

Your submit method references variables (entreprise, password, email) that don't exist in its scope, and it doesn't actually do anything useful anyway. You can delete this method entirely—we'll access widget values directly in the add_user method.

3. Mismatched SQL Query Parameters

Your INSERT statement has 3 columns (user, password, email) but only 2 placeholders (?,?). This will throw a database error that can disrupt app execution. Correct the query to use 3 placeholders:

cur.execute("INSERT INTO id (user, password, email) VALUES (?,?,?)", 
            (self.entreprise.text, self.password.text, self.email.text))

4. Ensure Your KV File Has a Valid Structure

Kivy can't create a window if there's no screen to display. Your my.kv file must define the WindowManager and link the CreateAccount screen's widgets to your ObjectProperty declarations. Here's a minimal working my.kv example:

<WindowManager>:
    CreateAccount:

<CreateAccount>:
    entreprise: entreprise_input
    email: email_input
    password: password_input

    BoxLayout:
        orientation: "vertical"
        padding: 20
        spacing: 10
        
        Label:
            text: "Company Name"
        TextInput:
            id: entreprise_input
            hint_text: "Enter your company name"
        
        Label:
            text: "Email"
        TextInput:
            id: email_input
            hint_text: "Enter your email"
        
        Label:
            text: "Password"
        TextInput:
            id: password_input
            hint_text: "Enter a password"
            password: True
        
        Button:
            text: "Create Account"
            on_press: root.add_user()

Fixed Full Python Code

Here's the complete corrected script, with added input validation and error handling to make your app more robust:

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

class CreateAccount(Screen):
    entreprise = ObjectProperty(None)
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    def add_user(self):
        # Basic input validation to avoid empty entries
        if not self.entreprise.text or not self.password.text or not self.email.text:
            Popup(title="Error", content=Label(text="All fields are required!"), size_hint=(0.5, 0.3)).open()
            return
        
        try:
            con = sql.connect("user.db")
            cur = con.cursor()
            # Uncomment this line on first run to create the table, then comment it again
            # cur.execute("CREATE TABLE IF NOT EXISTS id(user text, password text, email text)")
            cur.execute("INSERT INTO id (user, password, email) VALUES (?,?,?)", 
                        (self.entreprise.text, self.password.text, self.email.text))
            con.commit()
            
            # Show success popup and clear inputs
            Popup(title="Success", content=Label(text="Account created!"), size_hint=(0.5, 0.3)).open()
            self.entreprise.text = ""
            self.password.text = ""
            self.email.text = ""
        except Exception as e:
            Popup(title="Database Error", content=Label(text=str(e)), size_hint=(0.5, 0.3)).open()
        finally:
            con.close()

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

class WindowManager(ScreenManager):
    pass

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

if __name__ == "__main__":
    MyMainApp().run()

How to Test

  1. Place your my.kv file in the same folder as your Python script.
  2. On the first run, uncomment the table creation line in add_user to set up your database (then comment it for future runs).
  3. Run the script—you should see a window with the account creation form appear immediately.

内容的提问来源于stack exchange,提问作者H_D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:11:36