所以我遇到的问题是,如果应用程序中有 TextInput 字段,我的 Kivy 应用程序将无法启动。 (我使用的是Pycharm社区版)
只有标签的应用程序就可以正常运行。
没有错误,只是应用程序的窗口不会像仅使用标签时那样弹出。
这是我正在尝试运行的代码:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text='User Name'))
self.username = TextInput(multiline=False)
self.add_widget(self.username)
self.add_widget(Label(text='password'))
self.password = TextInput(password=True, multiline=False)
self.add_widget(self.password)
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()
这是运行此代码时的控制台日志:
[INFO ] [Logger ] Record log in /home/camcamyoung125/.kivy/logs/kivy_23-11-23_99.txt
[INFO ] [Kivy ] v2.2.1
[INFO ] [Kivy ] Installed at "/home/camcamyoung125/.local/lib/python3.11/lib/python3.11/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.11.3 (main, Jul 23 2023, 15:10:19) [GCC 10.2.1 20210110]
[INFO ] [Python ] Interpreter at "/home/camcamyoung125/.local/lib/python3.11/bin/python3.11"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 190 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2 (img_pil, img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'3.1 Mesa 21.2.6'>
[INFO ] [GL ] OpenGL vendor <b'Mesa/X.org'>
[INFO ] [GL ] OpenGL renderer <b'virgl (Mesa Intel(R) UHD Graphics (CML GT2))'>
[INFO ] [GL ] OpenGL parsed version: 3, 1
[INFO ] [GL ] Shading version <b'1.40'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <16>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
控制台日志就到此结束,应用程序窗口不会弹出。
我尝试过弄乱解释器,但据我所知,这不是解释器问题,因为 Kivy 在不使用 TextInputs 时运行良好。
我可以尝试的任何建议或步骤将不胜感激。谢谢!
编辑
原来问题出在 Pycharm 上。使用不同的 IDE(例如 Visual Code)对我来说是一种解决方法。
通常 IDE 不应该改变 Kivy 或您使用的任何其他库的行为。 IDE 的更改可能会修改您当前的 Python 环境(改为 VSCode),从而使您的代码能够运行。
我可以确认代码在 Pycharm 和 VSCode 中都能完美运行。默认情况下,Pycharm 使用自己的虚拟环境,与 VSCode 相反;这可能是导致问题的原因。
这是我使用 Kivy 库 2.2.1 的 Pycharm for Python 3.11 的完整日志:
[INFO ] [Logger ] Record log in C:\Users\Agathe\.kivy\logs\kivy_23-12-16_64.txt
[INFO ] [deps ] Successfully imported "kivy_deps.angle" 0.3.3
[INFO ] [deps ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO ] [deps ] Successfully imported "kivy_deps.sdl2" 0.6.0
[INFO ] [Kivy ] v2.2.1
[INFO ] [Kivy ] Installed at "H:\Documents\Private\Paul\Programmation\StackOverFlow\venv\Lib\site-packages\kivy\__init__.py"
[INFO ] [Python ] v3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "H:\Documents\Private\Paul\Programmation\StackOverFlow\venv\Scripts\python.exe"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 190 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2 (img_pil, img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] Backend used <glew>
[INFO ] [GL ] OpenGL version <b'4.6.0 NVIDIA 527.56'>
[INFO ] [GL ] OpenGL vendor <b'NVIDIA Corporation'>
[INFO ] [GL ] OpenGL renderer <b'NVIDIA GeForce GTX 1060/PCIe/SSE2'>
[INFO ] [GL ] OpenGL parsed version: 4, 6
[INFO ] [GL ] Shading version <b'4.60 NVIDIA'>
[INFO ] [GL ] Texture max size <32768>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Base ] Start application main loop
[INFO ] [GL ] NPOT texture support is available