Kivy应用Button与TextInput交互时出现纹理故障求助
Hey there, sorry to hear you're running into this frustrating texture glitch with your Kivy app—those rendering issues can be tricky! Let's walk through what's likely causing it and how to fix it.
First, Let's Recap Your Issue
Your app loads fine, with all buttons and text inputs displaying normally. But after interacting with any button or text input, every Button and TextInput in the app gets messed up visually. You tried switching all layouts to FloatLayout, but that didn't resolve the problem.
Glitch Comparison
- Before Interaction:

- After Interaction:

Common Causes & Fixes
This kind of texture corruption usually stems from Kivy's caching system, UI thread blocking, or outdated rendering logic. Here are the most effective fixes to try:
1. Disable Texture Caching for Affected Widgets
Kivy caches widget textures by default to boost performance, but sometimes this cache gets corrupted during screen transitions or interactions. Disabling it for Buttons and TextInputs often fixes the glitch.
Add this to the top of your MainApp.kv file:
<Button>: cache: False <TextInput>: cache: False
This tells Kivy not to cache textures for these widgets, preventing the corrupted renderings after interaction.
2. Move Database Operations to a Separate Thread
Looking at your code, all SQLite operations run on the main UI thread. When you perform database calls (like login checks or user registration), you're blocking Kivy's UI rendering loop, which can cause visual artifacts like texture glitches.
Let's modify your login logic to use a background thread:
# Update HandwritingRecognition.py from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen from kivy.clock import mainthread import DatabaseManagement import threading DatabaseManagement.Create() USER = '' class Login_Screen(Screen): def Login(self): global USER USER = self.ids.Username.text username = self.ids.Username.text password = self.ids.Passwrd.text # Run database check in a background thread threading.Thread(target=self._verify_login, args=(username, password)).start() def _verify_login(self, username, password): Found = DatabaseManagement.Find_User(username, password) # Update UI on main thread (required for Kivy) self._handle_login_result(Found) @mainthread def _handle_login_result(self, Found): if Found: print("Direct Entry") self.manager.current = "Login_Successful" else: print("Wrong Password") self.manager.current = "Login_Failed" # ... rest of your Login_Screen code # Update DatabaseManagement.py's Find_User function def Find_User(username, password): finduser = 'SELECT * FROM Users WHERE Username = ? AND Passwrd = ?' c.execute(finduser, (username, password)) data = c.fetchall() return bool(data)
Do the same for your Data_Entry method in the Registration_Screen—wrap it in a thread to keep the UI responsive.
3. Update Kivy or Switch Render Backend
Sometimes this is a bug in older Kivy versions. First, try upgrading to the latest stable version:
pip install --upgrade kivy
If that doesn't work, try switching Kivy's OpenGL backend. You can do this by setting an environment variable before running your app:
# On Linux/macOS export KIVY_GL_BACKEND=sdl2 # On Windows (Command Prompt) set KIVY_GL_BACKEND=sdl2
Or you can set it programmatically in your app:
class MainApp(App): def build(self): from kivy.core.window import Window Window.canvas = Window.canvas.__class__(Window) return RootWidget()
4. Clean Up Global Variables & Screen References
Using global variables like USER and passing entire Screen instances to your database functions can create unexpected reference cycles. This can prevent Kivy from properly cleaning up old widgets, leading to texture issues.
Instead of passing the Screen to Find_User, just pass the username and password values (like we did in fix #2). This keeps your code cleaner and avoids potential memory leaks.
Final Notes
Start with fix #1 (disabling caching)—it's the quickest to test and often resolves this exact issue. If that doesn't work, move to fix #2 (threading database operations), as blocking the UI thread is a common culprit for rendering glitches.
内容的提问来源于stack exchange,提问作者patryk loda

