我有一个简单的 Kivy 界面,也使用终端。
示例代码:
import kivy
kivy.require('1.0.6')
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text = 'Hello')
MyApp().run()
问题是,每当我启动脚本时,我都会得到以下信息:
[INFO ] [Logger ] Record log in C:\Users\Simon\.kivy\logs\kivy_18-05-12_37.txt
[INFO ] [Kivy ] v1.10.0
[INFO ] [Python ] v3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)]
[INFO ] [Factory ] 194 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [OSC ] using <thread> for socket
[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.5.13467 Compatibility Profile Context 21.19.414.1280'>
[INFO ] [GL ] OpenGL vendor <b'ATI Technologies Inc.'>
[INFO ] [GL ] OpenGL renderer <b'AMD Radeon R7 Graphics'>
[INFO ] [GL ] OpenGL parsed version: 4, 5
[INFO ] [GL ] Shading version <b'4.50'>
[INFO ] [GL ] Texture max size <16384>
[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
[INFO ] [Base ] Leaving application in progress...
我发现这是Kivy的自动调试信息。我如何阻止它们(或者至少隐藏起来以便我可以使用终端)?
如文档所示,您应该只执行以下操作:
import os
os.environ["KIVY_NO_CONSOLELOG"] = "1"
import kivy
...
现在您可以通过设置
Config
来设置您想要查看哪种日志。
您可以选择 trace
、debug
、info
、warning
、error
或 critical
之一
from kivy.config import Config
# your code ...
if __name__ == '__main__':
# path of the log directory
Config.set('kivy', 'log_dir', 'your_chosen_log_dir_path')
# filename of the log file
Config.set('kivy', 'log_name', "anything_you_want_%y-%m-%d_%_.log")
# Keep log_maxfiles recent logfiles while purging the log directory. Set ‘log_maxfiles’ to -1 to disable logfile purging (eg keep all logfiles).
Config.set('kivy', 'log_maxfiles', 1000)
# minimum log level which is what you need to not see kivy's default info logs
Config.set('kivy', 'log_level', 'error')
# apply all these changes
Config.write()
请记住,设置配置变量并运行程序不会立即停止默认调试消息。您在第一次运行时仍然会看到它们。这是因为 Kivy 在第一次运行时设置了配置变量,然后在接下来的运行中应用它。因此,您需要运行两次才能看到效果。
设置
os.environ["KIVY_NO_CONSOLELOG"] = "1"
确实会删除调试消息,但它也不允许您查看错误和关键消息。因此,如果您想查看错误和回溯,设置它不是一个明智的选择。
来源:
Kivy 官方对所有配置设置的解释。
Kivy官方对Logger对象的解释
找到config.py文件的路径: