python 我如何打破一个按钮成更小的按钮在kivy(网格布局)没有改变屏幕

h7appiyu  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(137)

例如,下面是代码
.py文件

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

class ScreenOne(Screen):
   pass
class WindowManager(ScreenManager):
   pass

kv = Builder.load_file("testing.kv")

class ScreenApp(App):
    def build(self):
        return kv

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

.kv恶意

WindowManager:
    ScreenOne:

<ScreenOne>:
    GridLayout:
        size: root.height, root.width
        rows: 2
        Button:
            text: "Break this button into 3 smaller buttons"
        Label:
            text: ""

当按钮被点击的时候,我想把按钮分成3个小按钮。你能帮我做一个on_release命令来做吗?

fjnneemd

fjnneemd1#

<ScreenOne>:
    GridLayout:
        btn: remove
        size: root.height, root.width
        rows: 2
        Button:
            id: remove
            text: "Break this button into 3 smaller buttons"
            on_press: root.break_into_three()

然后在ScreenOne类中创建一个名为break_into_three的函数,并将其定义如下:

class ScreenOne(Screen):
   
    def break_into_three(self):
        self.add_widget(Button(text='Button1'))
        self.add_widget(Button(text='Button2'))
        self.add_widget(Button(text='Button3'))
        self.remove_widget(self.btn)  # removes the initial Button

我没有一个工作的Kivy环境来测试这一点,但我想你明白了。

相关问题