我遇到的问题是应用程序在其他任何事情发生之前立即运行按钮的 on_press 命令。如果我使用 .kv 作为布局,它可以正常工作,但我希望能够使用简单的列表来管理按钮。
class AppBase(Widget):
def Launcher(self, launchapp):
os.system(launchapp)
def BuildLayout(self):
layout = GridLayout( rows=4, row_force_default = True, row_default_height = 100, col_force_default = True, col_default_width = 300 )
with open('config.txt', 'rb') as f:
reader = csv.reader(f, delimiter="|")
for row in reader:
launchbutton = Button( text = row[0], background_normal = 'tile.png', on_press = self.Launcher(row[1]) )
layout.add_widget(launchbutton)
return layout
class MyApp(App):
def build(self):
Config.set('graphics', 'width', 1920)
Config.set('graphics', 'height', 400)
return AppBase().BuildLayout()
if __name__ == '__main__':
MyApp().run()