【发布时间】: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)
【问题讨论】:
-
显示你的.kv.....
-
线程中的解决方案似乎不适合我的问题,因为我试图在 load 函数中调用 id 而不是 show_load 函数。用我的 .kv 编辑