【问题标题】:get the id of button from kv language file从kv语言文件中获取按钮的id
【发布时间】:2018-04-06 00:00:51
【问题描述】:

我在 kv 语言文件中设置了所有布局。然后当按下按钮时,它会调用一个文件选择器的弹出窗口。因为我有两个使用 filechooser 功能的按钮,所以我想设置一个 if 语句来根据按下的按钮的 id 做不同的事情。

类似的东西

#the onpress function
def show_load(self):
    content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
    self._popup = Popup(title="Load file", content=content,
                        size_hint=(0.9, 0.9))
    self._popup.open()
#store the path
def load(self,path,filename):
    global newfilepath
    global oldfilepath
    if buttonid==newfile
       newfilepath=os.path.join(path, filename[0])
    else 
       oldfilepath=os.path.join(path, filename[0])
    self.dismiss_popup()

我正在努力解决如何在加载功能时获取按钮 ID。我尝试了 self.ids 但这会在屏幕上生成所有小部件,而不是按下的那个。

.KV 文件

<checker_ui>:
    rows:2
    cols:1
    padding: 10
    spacing: 10
    BoxLayout:
        size_hint_y: None
        height: self.minimum_height
        Button:
            id:this_week_btn
            text: 'This Week Report'
            size_hint:(1, None)
            height: root.height/12
            on_release: root.show_load(self)
        Button:
            id:last_week_btn
            text: 'Last Week Report'
            size_hint:(1, None)
            height: root.height/12
            on_release: root.show_load(self)
        Button:
            id:confirm_btn
            text: 'Start Checking'
            size_hint:(1, None)
            height: root.height/12

    BoxLayout:
        Label:
            id:entry
            text:'Select This Week\'s report'
            font_size:18
            multiline:True
            canvas.before:
                Color:
                    rgba: 1, .5, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size

<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

【问题讨论】:

  • 你检查过这个吗?:stackoverflow.com/questions/42841321/…
  • 显示你的.kv.....
  • 线程中的解决方案似乎不适合我的问题,因为我试图在 load 函数中调用 id 而不是 show_load 函数。用我的 .kv 编辑

标签: python kivy


【解决方案1】:

解决方案

如果您在 show_load 方法中打印 按钮的 id,它将显示 None,因此您必须保存按钮的文本并使用它在 load 方法中。

Referencing Widgets

Kv 语言提供了一种使用 id 的方法。把它们想象成 只能在 Kv 语言中使用的类级变量。

在 Python 代码中,要引用 id,可以使用 self.ids.id-name.textself.ids['id-name '].textself.obj-name.text 其中 obj-name 是一个 ObjectProperty 并连接到 id-name 在 Kv 文件中定义。

当您的 kv 文件被解析时,kivy 会收集所有标记为的小部件 id 并将它们放在这个 self.ids 字典类型属性中。

示例

片段 - main.py

​​>
class checker_ui(GridLayout):
    btn_text = StringProperty('')

    # the on_release function
    def show_load(self, instance):
        # save the button's text because button's id is None
        self.btn_text = instance.text
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    # store the path
    def load(self, path, filename):
        global newfilepath
        global oldfilepath
        if self.btn_text == newfile:
           newfilepath = os.path.join(path, filename[0])
        else:
           oldfilepath = os.path.join(path, filename[0])
        self.dismiss_popup()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多