Kivy - 错误无法获得权限?
Posted
技术标签:
【中文标题】Kivy - 错误无法获得权限?【英文标题】:Kivy - Error Unable permissions? 【发布时间】:2018-04-26 13:19:53 【问题描述】:我需要一些帮助。我正在学习kivy,我使用pycharm作为IDE进行开发。 为了使用 kivy,我创建了一个带有要求的 virtualenvs:
kivy = 1.10
Cython = 0.23
Python = 3.5
至于设置,我没有太多麻烦。后来当我尝试生成布局时它出现了,但它没有出现。 是我的代码:
# coding = utf-8
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.core.window import Window
def click():
print (ed.text)
def build():
layout = FloatLayout()
ed = TextInput(text="USER.COM")
global ed
ed.size_hint = None, None
ed.height = 300
ed.width = 400
ed.y = 60
ed.x = 250
bt = Button(text="Click Me")
bt.size_hint = None, None
bt.height = 50
bt.width = 200
bt.y = 150
bt.x = 170
bt.size_hint = None, None
bt.on_press = click()
layout.add_widget(ed)
layout.add_widget(bt)
return layout
Window.size=600,600
open= App()
open.title = "USER_FREE_AS"
open.buid=build
open.run()
日志报告不允许pycharm访问该函数。是我的日志:
/home/user/.virtualenvs/k35/bin/python /home/user/Projetos/Python/kivy/source/tela_layout/main.py
/home/user/Projetos/Python/kivy/source/tela_layout/main.py:18: SyntaxWarning: name 'ed' is assigned to before global declaration
global ed
[INFO ] [Logger ] Record log in /home/victorpenna/.kivy/logs/kivy_17-11-13_11.txt
[INFO ] [Kivy ] v1.10.0
[INFO ] [Python ] v3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609]
[INFO ] [Factory ] 194 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [OSC ] using <multiprocessing> for socket
[INFO ] [Window ] Provider: sdl2(['window_egl_rpi'] ignored)
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used <gl>
[INFO ] [GL ] OpenGL version <b'3.0 Mesa 17.3.0-rc2 - padoka PPA'>
[INFO ] [GL ] OpenGL vendor <b'Intel Open Source Technology Center'>
[INFO ] [GL ] OpenGL renderer <b'Mesa DRI Intel(R) HD Graphics 5500 (Broadwell GT2) '>
[INFO ] [GL ] OpenGL parsed version: 3, 0
[INFO ] [GL ] Shading version <b'1.30'>
[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 ] [ProbeSysfs ] device match: /dev/input/event8
[INFO ] [MTD ] Read event from </dev/input/event8>
[INFO ] [ProbeSysfs ] device match: /dev/input/event13
[INFO ] [MTD ] Read event from </dev/input/event13>
[INFO ] [ProbeSysfs ] device match: /dev/input/event14
[INFO ] [MTD ] Read event from </dev/input/event14>
[INFO ] [Base ] Start application main loop
[WARNING] [MTD ] Unable to open device "/dev/input/event8". Please ensure you have the appropriate permissions.
[WARNING] [MTD ] Unable to open device "/dev/input/event13". Please ensure you have the appropriate permissions.
[WARNING] [MTD ] Unable to open device "/dev/input/event14". Please ensure you have the appropriate permissions.
[INFO ] [Base ] Leaving application in progress...
Process finished with exit code 0
我该如何解决这个问题?
P.S:我的 linux 是 ubuntu 16.04,Intel GPU。
【问题讨论】:
问题不在于权限,而是您在未声明的情况下使用了变量:SyntaxWarning: name 'ed' is assigned to before global declaration global ed。解决方案是global ed ed=TextInput(text="USER.COM")
错误依旧,无法调用layout独立命令插入全局编辑
你试过从终端运行它吗?
错误仍然继续,即使是终端!
所以问题不在于 PyCharm,而在于 kivy。
【参考方案1】:
暗窗
由于出现拼写错误,显示了一个深色窗口。
替换:
open.buid=build
与:
open.build=build
Kivy 文档
Programming Guide » Kivy Basics
Programming Guide » Kv language
设备权限
为了使用这些设备,您需要授予用户或组权限。这可以通过(在您的情况下,将“X”替换为 8、13、14):
$ sudo chmod u+r /dev/input/eventX
语法警告:名称“ed”
你应该在赋值之前声明全局变量ed。
ed = ""
def click():
print (ed.text)
def build():
global ed
layout = FloatLayout()
ed = TextInput(text="USER.COM")
输出
【讨论】:
。感谢您的建议,我会把它写下来作为 kivy 编程的良好实践【参考方案2】:你有一个错字:
open.buid=build
应该是
open.build=build
但不要那样做。之后将方法放入实例(monkeypatching)会很快变得非常混乱。在这种情况下,其他开发人员不会期望它。 kivy 还对应用程序的类名做了一些魔术,它会按照命名约定定位.kv
文件。所以改为写:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.core.window import Window
class MyApp(App):
def click(self):
print(self.ed.text)
def build(self):
layout = FloatLayout()
self.ed = TextInput(text="USER.COM")
ed.size_hint = None, None
ed.height = 300
ed.width = 400
ed.y = 60
ed.x = 250
bt = Button(text="Click Me")
bt.size_hint = None, None
bt.height = 50
bt.width = 200
bt.y = 150
bt.x = 170
bt.size_hint = None, None
bt.on_press = click()
layout.add_widget(ed)
layout.add_widget(bt)
return layout
Window.size=600,600
open= MyApp()
open.title = "USER_FREE_AS"
open.run()
这样您还可以避免使用全局变量,这在大多数情况下表明您做的事情并不理想。
如果您还是 kivy 新手,我建议您通过 kivy tutorials。
【讨论】:
非常感谢,伙计,在认真学习了几个小时后,我们让细节传递了。我正在阅读文档以便能够指导我。以上是关于Kivy - 错误无法获得权限?的主要内容,如果未能解决你的问题,请参考以下文章
Kivy 错误:[CRITICAL] [App] 无法获取窗口,中止