Kivy中程序生成的按钮

2024-09-26 22:11:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我希望我的按钮在释放时调用一个函数,这样我的标签就会显示出来。 正如您所看到的,按钮的on_release设置为buttonPress,我想在其中执行此操作,但无论我按下哪个按钮,它都会将最后一个标签传递到函数中。我怎样才能解决这个问题?有更好的方法吗

    def buttonPress(self, label):
        print("hi")
        print(label)
        label.height = '50dp'
        label.font_size = '20sp'


    def on_pre_enter(self, *args):
        mainLayout = RelativeLayout()
        self.add_widget(mainLayout)
        scroll = ScrollView(do_scroll_x = False,pos_hint={"top":0.8,"center_x":0.75})
        infoLayout = GridLayout(cols = 1, size_hint = (0.5, None))
        mainLayout.add_widget(scroll)
        scroll.add_widget(infoLayout)

        files = glob.glob("user/*.txt")
        InfoWidgetButtonArray = []
        InfoWidgetLabelArray = []

        for x in range(len(files)):
            InfoWidgetLabelArray.append(Label(text="hello mate", size_hint_y=None,height = '0dp', font_size='0sp', color = (0, 0, 0, 1), id=str(x)))
            print(InfoWidgetLabelArray[x])
            InfoWidgetButtonArray.append(Button(text=files[x][5:][:-4], font_size=14, size_hint=(0.5, None), height=30,on_release=lambda lambdaFunction: self.buttonPress(InfoWidgetLabelArray[x])))
            infoLayout.add_widget(InfoWidgetButtonArray[x])
            infoLayout.add_widget(InfoWidgetLabelArray[x])

以下是一个最小的可复制示例:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.uix.widget import Widget


Window.clearcolor = (1,1,1,1)

class InfoScreen(Screen, Widget):
    new = True
    def buttonPress(self, label):
        print("hi")
        print(label)
        label.height = '50dp'
        label.font_size = '20sp'


    def on_pre_enter(self, *args):
        mainLayout = RelativeLayout()
        self.add_widget(mainLayout)
        scroll = ScrollView(do_scroll_x = False,pos_hint={"top":0.8,"center_x":0.75})
        infoLayout = GridLayout(cols = 1, size_hint = (0.5, None))
        mainLayout.add_widget(scroll)
        scroll.add_widget(infoLayout)


        InfoWidgetButtonArray = []
        InfoWidgetLabelArray = []

        for x in range(2):
            InfoWidgetLabelArray.append(Label(text="hello mate", size_hint_y=None,height = '0dp', font_size='0sp', color = (0, 0, 0, 1), id=str(x)))
            print(InfoWidgetLabelArray[x])
            InfoWidgetButtonArray.append(Button(text="name"+str(x), font_size=14, size_hint=(0.5, None), height=30,on_release=lambda lambdaFunction: self.buttonPress(InfoWidgetLabelArray[x])))
            infoLayout.add_widget(InfoWidgetButtonArray[x])
            infoLayout.add_widget(InfoWidgetLabelArray[x])


        SpacingButton = Button(text='Hello world', font_size=14, size_hint=(0.5, None), height=50, color=(0, 0, 0, 0), background_color=(0,0,0,0))
        infoLayout.add_widget(SpacingButton)

        infoLayout.bind(minimum_height=infoLayout.setter('height'))



sm = ScreenManager()
sm.add_widget(InfoScreen(name='Info'))


class TestApp(App):
    def build(self):

        return sm

if __name__ == '__main__':
    TestApp().run()

Tags: fromimportselfnoneaddsizewidgetlabel
1条回答
网友
1楼 · 发布于 2024-09-26 22:11:40

我想您正在从functools模块中寻找partial函数。它绑定for循环中的输入,并返回您希望它具有的实际函数

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.properties import StringProperty

from functools import partial #Changed This

Window.clearcolor = (1,1,1,1)

class InfoScreen(Screen, Widget):
    new = True
    def buttonPress(self, label, *args):
        print("hi")
        print(label)
        label.height = '50dp'
        label.font_size = '20sp'

    def on_pre_enter(self, *args):
        mainLayout = RelativeLayout()
        self.add_widget(mainLayout)
        scroll = ScrollView(do_scroll_x = False, pos_hint={"top":0.8,"center_x":0.75}, size_hint_max_y=300) #To enable scrolling be sure to set a maximal value, or have enough buttons to cover the whole window
        infoLayout = GridLayout(cols = 1, size_hint = (0.5, None))
        mainLayout.add_widget(scroll)
        scroll.add_widget(infoLayout)


        InfoWidgetButtonArray = []
        InfoWidgetLabelArray = []

        for x in range(5):
            InfoWidgetLabelArray.append(Label(text="hello mate", size_hint_y=None,height = '0dp', font_size='0sp', color = (0, 0, 0, 1), id=str(x)))
            print(InfoWidgetLabelArray[x])
            InfoWidgetButtonArray.append(Button(text="name"+str(x), font_size=14, size_hint=(0.5, None), height=30, on_release=partial(self.buttonPress, InfoWidgetLabelArray[x]))) #Changed This
            infoLayout.add_widget(InfoWidgetButtonArray[x])
            infoLayout.add_widget(InfoWidgetLabelArray[x])


        SpacingButton = Button(text='Hello world', font_size=14, size_hint=(0.5, None), height=50, color=(0, 0, 0, 0), background_color=(0,0,0,0))
        infoLayout.add_widget(SpacingButton)

        infoLayout.bind(minimum_height=infoLayout.setter('height'))

sm = ScreenManager()
sm.add_widget(InfoScreen(name='Info'))

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

希望这就是你想要的,我花了一些时间重新阅读了这个问题,然后才有希望理解

相关问题 更多 >

    热门问题