在 gui 中按下鼠标时添加小部件 - kivy python
Add widget on mouse pressed in gui - kivy python
我每次使用鼠标按下屏幕时都无法在布局中添加和显示图像。
class Myszka(ClickAndGo, Widget):
def on_touch_down(self, touch):
super().build()
flaga_path = os.path.join(self.img_path, "test.png")
x, y = touch.pos
self.flaga = Image(source=flaga_path, size_hint=(None, None), size=(64, 64),
pos=(round(x, 1), round(y, 1)))
self.camlayout.add_widget(self.flaga)
print(touch.pos)
- 实际结果:
仅打印触摸位置,未显示图像。
- 预期结果:
每次按下鼠标时都应该显示图像。
@ikolim
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class ClickAndGo(App):
def build(self):
self.camlayout = FloatLayout(size=(100,100))
self.myszka = Myszka()
self.camlayout.add_widget(self.myszka)
return self.camlayout
class Myszka(ClickAndGo, Widget):
def on_touch_down(self, touch):
super().build()
# test.png -> any image
flaga_path = os.path.join(self.img_path, "test.png")
x, y = touch.pos
self.flaga = Image(source=flaga_path, size_hint=(None, None), size=(64, 64),
pos=(round(x, 1), round(y, 1)))
self.camlayout.add_widget(self.flaga)
print(touch.pos)
问题
I have problem with adding and showing image to the layout every time
I press the screen using mouse.
根本原因
图像未显示,因为它正在添加到 class Myszka()
方法 on_touch_down()
中的本地属性 self.camlayout
。
解决方案
将self.camlayout.add_widget(self.flaga)
替换为App.get_running_app().root.add_widget(self.flaga)
,即获取根实例(camlayout
)。
片段 - py
class Myszka(Widget):
def on_touch_down(self, touch):
...
App.get_running_app().root.add_widget(self.flaga)
例子
以下示例说明在鼠标点击FloatLayout
的位置添加Image
。
main.py
import os
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
class Mouse(FloatLayout):
def on_touch_down(self, touch):
img_path = "/home/iam/Pictures/AppImages"
flag_path = os.path.join(img_path, "Android_celebrate.png")
flag = Image(source=flag_path, size_hint=(None, None), size=(64, 64),
pos=(round(touch.pos[0], 1), round(touch.pos[1], 1)))
self.add_widget(flag)
class TestApp(App):
def build(self):
return Mouse()
if __name__ == "__main__":
TestApp().run()
输出
我每次使用鼠标按下屏幕时都无法在布局中添加和显示图像。
class Myszka(ClickAndGo, Widget):
def on_touch_down(self, touch):
super().build()
flaga_path = os.path.join(self.img_path, "test.png")
x, y = touch.pos
self.flaga = Image(source=flaga_path, size_hint=(None, None), size=(64, 64),
pos=(round(x, 1), round(y, 1)))
self.camlayout.add_widget(self.flaga)
print(touch.pos)
- 实际结果: 仅打印触摸位置,未显示图像。
- 预期结果: 每次按下鼠标时都应该显示图像。
@ikolim
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class ClickAndGo(App):
def build(self):
self.camlayout = FloatLayout(size=(100,100))
self.myszka = Myszka()
self.camlayout.add_widget(self.myszka)
return self.camlayout
class Myszka(ClickAndGo, Widget):
def on_touch_down(self, touch):
super().build()
# test.png -> any image
flaga_path = os.path.join(self.img_path, "test.png")
x, y = touch.pos
self.flaga = Image(source=flaga_path, size_hint=(None, None), size=(64, 64),
pos=(round(x, 1), round(y, 1)))
self.camlayout.add_widget(self.flaga)
print(touch.pos)
问题
I have problem with adding and showing image to the layout every time I press the screen using mouse.
根本原因
图像未显示,因为它正在添加到 class Myszka()
方法 on_touch_down()
中的本地属性 self.camlayout
。
解决方案
将self.camlayout.add_widget(self.flaga)
替换为App.get_running_app().root.add_widget(self.flaga)
,即获取根实例(camlayout
)。
片段 - py
class Myszka(Widget):
def on_touch_down(self, touch):
...
App.get_running_app().root.add_widget(self.flaga)
例子
以下示例说明在鼠标点击FloatLayout
的位置添加Image
。
main.py
import os
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
class Mouse(FloatLayout):
def on_touch_down(self, touch):
img_path = "/home/iam/Pictures/AppImages"
flag_path = os.path.join(img_path, "Android_celebrate.png")
flag = Image(source=flag_path, size_hint=(None, None), size=(64, 64),
pos=(round(touch.pos[0], 1), round(touch.pos[1], 1)))
self.add_widget(flag)
class TestApp(App):
def build(self):
return Mouse()
if __name__ == "__main__":
TestApp().run()