📜  kivy on_press 从 main 绑定 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:26.830000             🧑  作者: Mango

代码示例1
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivymd.uix.button import MDRaisedButton
from kivy.config import Config
import csv
import os

class AppBase(Widget):
    def Launcher(self, launchapp):
        #os.system(launchapp)
        print(f"Application to be launched is {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.csv', 'r') as f:
            reader = csv.reader(f, delimiter='|')
            for row in reader:
                print(row)
                launchbutton = MDRaisedButton(text = row[0])
                
                launchbutton.bind( on_press = lambda widget, appname=row[1]: self.Launcher( appname ) )
                layout.add_widget(launchbutton)
        return layout

class MyApp(MDApp):
    def build(self):
        Config.set('graphics','width', 1920)
        Config.set('graphics', 'height', 400)
        Config.set('graphics', 'maxfps', 30)
        return AppBase().BuildLayout()
if __name__ == '__main__':
    MyApp().run()