Kivy 两个按钮......如何让其中一个按钮在按下时移除另一个按钮?
Kivy Two Buttons... How to make one of the Buttons remove the other on pressing it?
我有这段代码,屏幕上有两个按钮,“普通按钮”和“删除按钮”。我需要在按下“删除按钮”时删除普通按钮...请帮助,需要它用于 imp 项目。
主要代码:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen,ScreenManager
class Main(Screen):
def remove(self):
self.remove_widget(self.btn) # I'm not sure what to put here
class Manager(ScreenManager):
pass
kv=Builder.load_file("btn.kv")
screen=Manager()
screen.add_widget(Main(name="main"))
class Test(App):
def build(self):
return screen
Test().run()
KV代码:
<Main>:
name: "main"
GridLayout:
id: GL
cols: 1
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.5}
Button:
id: btn
text: "Remove Button"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.6}
Button:
id: btn2
text: "Normal Button"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.4}
on_press:
root.remove()
首先,如果您希望 Remove Button
执行删除操作,则必须将 Remove Button
绑定到 remove()
方法:
Button:
id: btn
text: "Remove Button"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.6}
on_press:
root.remove(btn2)
Button:
id: btn2
text: "Normal Button"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.4}
您可以通过使用 btn2
id
作为 remove()
方法的参数将要删除的 Button
传递给 remove()
方法。
然后您可以在 remove()
方法中使用该参数:
class Main(Screen):
def remove(self, butt):
gl = self.ids.GL
if butt in gl.children:
gl.remove_widget(butt)
else:
print('already removed')
我有这段代码,屏幕上有两个按钮,“普通按钮”和“删除按钮”。我需要在按下“删除按钮”时删除普通按钮...请帮助,需要它用于 imp 项目。
主要代码:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen,ScreenManager
class Main(Screen):
def remove(self):
self.remove_widget(self.btn) # I'm not sure what to put here
class Manager(ScreenManager):
pass
kv=Builder.load_file("btn.kv")
screen=Manager()
screen.add_widget(Main(name="main"))
class Test(App):
def build(self):
return screen
Test().run()
KV代码:
<Main>:
name: "main"
GridLayout:
id: GL
cols: 1
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.5}
Button:
id: btn
text: "Remove Button"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.6}
Button:
id: btn2
text: "Normal Button"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.4}
on_press:
root.remove()
首先,如果您希望 Remove Button
执行删除操作,则必须将 Remove Button
绑定到 remove()
方法:
Button:
id: btn
text: "Remove Button"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.6}
on_press:
root.remove(btn2)
Button:
id: btn2
text: "Normal Button"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.4}
您可以通过使用 btn2
id
作为 remove()
方法的参数将要删除的 Button
传递给 remove()
方法。
然后您可以在 remove()
方法中使用该参数:
class Main(Screen):
def remove(self, butt):
gl = self.ids.GL
if butt in gl.children:
gl.remove_widget(butt)
else:
print('already removed')