python - 使用 on_touch_up 从下拉菜单选项中实例化数据类
问题描述
我想用从下拉菜单中收集的信息填充 MyData,这些信息显示在 AddTouch 中带有“on_touch_up”的弹出窗口中。除了下拉数据之外,该数据还包括“on_touch_up”的位置。我可以在 AddTouch 类中打印位置,但是我很难在我的脚本中进一步获取数据(例如: print('from MyMainApp: {}'.format(MyData.pos))) .
我也无法让“主按钮”或“下拉菜单”显示在弹出窗口中。
解决这个问题我想出了以下可行的方法,但不能满足我的需要
.py
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.dropdown import DropDown
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.graphics import Color, Rectangle
class AddTouch(Widget):
def __init__(self, **kwargs):
super(AddTouch, self).__init__(**kwargs)
with self.canvas:
Color(1, 0, 0, 0.5, mode="rgba")
self.rect = Rectangle(pos=(0, 0), size=(10, 10))
def on_touch_down(self, touch):
self.rect.pos = touch.pos
def on_touch_move(self, touch):
self.rect.pos = touch.pos
def on_touch_up(self, touch):
# final position
self.pos = touch.pos
print(self.pos)
class MyPopup(Popup):
def __init__(self, **kwargs):
super(MyPopup, self).__init__(**kwargs)
# create a main button
self.mainbutton = Button(text='Hello', size_hint=(None, None))
# create a dropdown with 10 buttons
self.dropdown = DropDown()
for index in range(10):
btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
self.dropdown.add_widget(btn)
self.mainbutton.bind(on_release=self.dropdown.open)
self.dropdown.bind(on_select=lambda instance, x: setattr(self.mainbutton, 'text', x))
class MyData:
def __init__(self, **kwargs):
super(MyData, self).__init__(**kwargs)
self.pos=AddTouch.pos
# using kivy screen for consistency
class MainWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("dropd.kv")
class MyMainApp(App):
def build(self):
return kv
print('from MyMainApp: {}'.format(MyData.pos))
if __name__ == "__main__":
MyMainApp().run()
.kv
WindowManager:
MainWindow:
<MainWindow>:
name: "main"
AddTouch:
on_touch_up:
#MyPopup gives 'MyPopup' is not defined, even if I add <MyPopup>: below
#root.MyPopup gives 'MainWindow' object has no attribute 'MyPopup'
我尝试基于此在 .kv 文件中添加一个简单的动态 Popup 类,但它再次显示未定义“MyPopup”:
.kv
AddTouch
on_touch_up:
MyPopup
<MyPopup@Popup>:
auto_dismiss: False
Button:
text: 'Close me!'
on_release: root.dismiss()
我缺少什么(除了经验、能力和一般智力)?
解决方案
除了添加该行:
self.content = self.mainbutton
对于该方法,您可以通过将文件修改为MyPopup
__init__()
来触发MyPopup
创建:.kv
#:import Factory kivy.factory.Factory
WindowManager:
MainWindow:
<MainWindow>:
name: "main"
AddTouch:
on_touch_up:
Factory.MyPopup().open()
推荐阅读
- python - 试图让 Selenium 下载基于 JavaScript 的数据......我想
- r - Change aestesics in ggplot2 barplot
- angular - 如何让用户在 Firebase 中将图像文件设置为私有或公开?
- c++ - 在模板类中声明非模板函数?
- r - 来自df中多列的grep向量并在R中打印第n列
- c# - 来自 Assembly 的 Autofac 寄存器类型
- rest - HttpClient.post 的离子问题(不存在“Access-Control-Allow-Origin”标头)
- sql - 在 Oracle/H2 中以标准方式规范化或存储 SQL 时间戳
- ios - Swift AWSPolly 中的错误处理
- powershell - 如何检索原始的、未解析的(未扩展的)注册表值?