python - 在 kivy python 中使用画布创建水平和垂直滚动视图
问题描述
我正在尝试使用 Kivy 框架在 python 中创建一个水平和垂直的 ScrollView。
最终目标是我试图制作类似于 photoshop 或 gimp 的 Canvas 部分的东西,我可以在其中将绘图添加到画布中,并且可以垂直或水平滚动。
但到目前为止我无法做到这一点。我在互联网上找不到任何方法。
这是我到目前为止得到的
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
class MyScrollView(ScrollView):
def on_touch_down(self, touch):
# get touch position in coordinates of the main_box (parent of inner_scroll)
x,y = self.ids.main_box.to_widget(*self.to_window(*touch.pos))
# if converted position is within the inner_scroll, send touch to the inner_scroll
if self.ids.inner_scroll.collide_point(x,y):
touch.pos = (x,y) # change touch position to coordinates in main_box
return self.ids.inner_scroll.on_touch_down(touch) # call on_touch of inner_scroll
else:
return super(MyScrollView, self).on_touch_down(touch)
theRoot = Builder.load_string('''
#: import Window kivy.core.window.Window
MyScrollView:
bar_width: 5
scroll_type:['bars', 'content']
do_scroll: (False, True)
size_hint_y: None
height: Window.height
GridLayout:
id: main_box
size_hint_y: None
cols: 1
height: self.minimum_height
ScrollView:
id: inner_scroll
bar_width: 5
scroll_type: ['bars', 'content']
do_scroll: (True, False)
size_hint_y: None
effect_cls: "ScrollEffect"
height: Window.height/4.5
GridLayout:
id: horizontal_grid
rows: 1
padding: [10, 10]
size: self.minimum_size
size_hint: None, None
''')
class ScrollTwoApp(App):
def build(self):
Clock.schedule_once(self.add_members)
return theRoot
def add_members(self, dt):
for i in range(25):
theRoot.ids.main_box.add_widget(Label(text='label'+str(i), size_hint=(1.0, None), height=25))
theRoot.ids.horizontal_grid.add_widget(Label(text='Hlabel' + str(i), size_hint=(None, 1), width=75))
ScrollTwoApp().run()
但这是我想要实现的目标
解决方案
推荐阅读
- powerquery - Power Query如何对以逗号分隔的值单元格求和
- python - 如何在没有撇号的情况下在python中保存csv中的字符串列表
- javascript - react fetch 响应类型的含义
- javascript - 在 React 中将 props 传递给动态渲染的组件
- python - 是否有在 Python 中的 AllenNLP 中执行共指解析的代码?
- qt - 关于在 Qt 中设置小部件父级的问题
- javascript - Bootstrap Grid Row 如果隐藏则不对齐中心然后用 JS 显示
- javascript - 在 Google、Facebook 或 Twitter 提供商和 Firebase Emulators Suite 中使用 signInWithPopup 时出现“无效 IdP 响应/凭据”错误
- sql - 在 SQL 中查找两个字符串之间的差异
- python-3.x - 使用 pexpect,是否有一种简单的方法来获取多个事件(将其与普通 I/O 结合)?