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

Kivy密码管理器黑屏问题求助:新增/编辑/删除数据时异常

Kivy密码管理器黑屏及数据操作失效问题

开发一款基于Kivy框架的个人密码管理器时遇到以下问题:

  • 输入用户名和密码新增数据时出现黑屏,必须重启程序才能恢复显示
  • 执行编辑或删除操作时同样会黑屏,且数据并未完成对应的编辑/删除操作

尝试通过ChatGPT解决但未成功,对方称此为死胡同。相关代码及问题截图如下:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.scrollview import ScrollView
from cryptography.fernet import Fernet
import sqlite3


# Connect to SQLite database and create table to store usernames and passwords
conn = sqlite3.connect('passwords.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users (username BLOB, password BLOB)''')
conn.commit()
# Initialize Fernet object with a random encryption key
key = b'tsVJf7wyGdM6CzK4yJ528QiNgWgideesHJxB0xdqE7s='

fernet = Fernet(key)


class PasswordManagerApp(App):
    def build(self):
        # Create main layout
        main_layout = BoxLayout(orientation='vertical')

        # Create scrollview to display usernames and passwords
        scrollview = ScrollView()
        scrollview_layout = BoxLayout(orientation='vertical', size_hint_y=None)
        scrollview_layout.bind(minimum_height=scrollview_layout.setter('height'))
        scrollview.add_widget(scrollview_layout)

        # Display existing usernames and passwords, or message if none exist
        c.execute("SELECT * FROM users")
        rows = c.fetchall()
        if rows:
            for row in rows:
                username = fernet.decrypt(row[0]).decode()
                password = fernet.decrypt(row[1]).decode()
                credentials_label = Label(text=f'{username: <20} |      {password}', size_hint_y=None, height=50)
                show_button = Button(text='Show', size_hint_x=None, width=100)
                show_button.bind(on_press=lambda instance, u=username, p=password, button=show_button: self.show_popup(u, p, button))

                credentials_layout = BoxLayout(orientation='horizontal')
                credentials_layout.add_widget(credentials_label)
                credentials_layout.add_widget(show_button)
                scrollview_layout.add_widget(credentials_layout)
        else:
            no_data_label = Label(text='No data found.')
            scrollview_layout.add_widget(no_data_label)

        # Create button to add new username and password
        add_button_layout = BoxLayout(orientation='horizontal', size_hint_y=None, height=50)
        username_input = TextInput(hint_text='Username')
        password_input = TextInput(hint_text='Password')
        add_button = Button(text='Add', size_hint_x=None, width=100)
        add_button.bind(on_press=lambda instance: self.save_credentials(username_input.text, password_input.text))
        add_button_layout.add_widget(username_input)
        add_button_layout.add_widget(password_input)
        add_button_layout.add_widget(add_button)

        # Add widgets to main layout
        main_layout.add_widget(scrollview)
        main_layout.add_widget(add_button_layout)

        return main_layout


    def save_credentials(self, username, password):
        # Check if username or password is empty
        if not username or not password:
            error_popup = Popup(title='Error', content=Label(text='Please enter a username and password.'), size_hint=(None, None), size=(400, 200))
            error_popup.open()
            return

        # Encrypt username and password using Fernet
        username = fernet.encrypt(username.encode())
        password = fernet.encrypt(password.encode())

        # Store encrypted username and password in database
        c.execute("INSERT INTO users VALUES (?, ?)", (username, password))
        conn.commit()

        # Create and display success message popup
        success_popup = Popup(title='Success', content=Label(text='Credentials saved successfully.'), size_hint=(None, None), size=(400, 200))
        success_popup.open()

        # Refresh display to show newly added username and password
        self.root.clear_widgets()
        self.build()
    def show_popup(self, username, password, button):
    # Create popup layout
        popup_layout = BoxLayout(orientation='vertical')
        self.current_button = button
        # Add labels for username and password
        username_label = Label(text=f'Username: {username}')
        password_label = Label(text=f'Password: {password}')
        popup_layout.add_widget(username_label)
        popup_layout.add_widget(password_label)

        # Add buttons to edit and delete credentials
        edit_button = Button(text='Edit', size_hint_y=None, height=50)
        delete_button = Button(text='Delete', size_hint_y=None, height=50)

        # Function to handle editing credentials
        def edit_credentials(instance):
            # Close the popup
            popup.dismiss()

            # Create popup layout for editing
            edit_layout = BoxLayout(orientation='vertical')

            # Add text inputs for new username and password
            new_username_input = TextInput(hint_text='New username')
            new_password_input = TextInput(hint_text='New password')
            edit_layout.add_widget(new_username_input)
            edit_layout.add_widget(new_password_input)

            # Add button to save changes
            save_button = Button(text='Save', size_hint_y=None, height=50)

            # Function to handle saving changes
            def save_changes(instance):
                # Check if new username or password is empty
                if not new_username_input.text or not new_password_input.text:
                    error_popup = Popup(title='Error', content=Label(text='Please enter a new username and password.'), size_hint=(None, None), size=(400, 200))
                    error_popup.open()
                    return

                # Encrypt new username and password
                new_username = fernet.encrypt(new_username_input.text.encode())
                new_password = fernet.encrypt(new_password_input.text.encode())

                # Update database with new encrypted credentials
                c.execute("UPDATE users SET username=?, password=? WHERE username=? AND password=?", (new_username, new_password, fernet.encrypt(username.encode()), fernet.encrypt(password.encode())))
                conn.commit()

                # Create and display success message popup
                success_popup = Popup(title='Success', content=Label(text='Credentials updated successfully.'), size_hint=(None, None), size=(400, 200))
                success_popup.open()

                # Refresh display to show updated credentials
                self.root.clear_widgets()
                self.build()

            save_button.bind(on_press=save_changes)
            edit_layout.add_widget(save_button)

            # Create and display edit popup
            edit_popup = Popup(title='Edit credentials', content=edit_layout, size_hint=(None, None), size=(400, 200))
            edit_popup.open()

        edit_button.bind(on_press=edit_credentials)
        popup_layout.add_widget(edit_button)

        # Function to handle deleting credentials
        def delete_credentials(instance):
            # Delete encrypted credentials from database
            c.execute("DELETE FROM users WHERE username=? AND password=?", (fernet.encrypt(username.encode()), fernet.encrypt(password.encode())))
            conn.commit()

            # Create and display success message popup
            success_popup = Popup(title='Success', content=Label(text='Credentials deleted successfully.'), size_hint=(None, None), size=(400, 200))
            success_popup.open()

            # Refresh display to show updated list of credentials
            self.root.clear_widgets()
            self.build()

        delete_button.bind(on_press=delete_credentials)
        popup_layout.add_widget(delete_button)

        # Create and display popup
        popup = Popup(title='Credentials', content=popup_layout, size_hint=(None, None), size=(400, 200))
        popup.open()


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

Kivy密码管理器黑屏截图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 00:07:03