当用户想要在 kivy 中显示小部件
Display widget when the user wants to in kivy
这是一个简单的程序,但我找不到让它工作的方法。我只想在用户按下位于 boxlayout1 中的按钮(并且没有在 textinput 中写入任何内容)时在 boxlayout2 中添加一个小部件。小部件不显示在 screen.What 中,我应该怎么做?
main.py
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class BoxLayout1(BoxLayout):
def Search(self):
if self.ids.textinput.text!='':
BoxLayout2()
class BoxLayout2(BoxLayout):
def Print(self):
self.add_widget(Button(text='hello'))
class TestApp(App):
pass
TestApp().run()
这是我的 kivy 代码
test.kv
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
我看到了我想要的演示文稿布局,但找不到按钮。
为了清楚起见,让我们跟随您编写的应用程序的流程。
- 它创建了一个BoxLayout 并将BoxLayout1 和BoxLayout2 放入其中,第二个没有任何内容。当您点击写入时,应用程序会检查文本框的内容,如果有效,则调用 BoxLayout2 的构造函数!现在,它创建了这个 class 的一个实例,但不保留它的引用,所以它会立即被丢弃。现在你想要的是调用一个当前存在的实例的函数,而不是创建另一个实例。这是代码:
python:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class BoxLayout1(BoxLayout):
def Search(self):
if self.ids.textinput.text!='':
self.parent.ids.bxl2.addButton()
# BoxLayout2()
class BoxLayout2(BoxLayout):
def addButton(self):
button=Button(text='hello')
self.add_widget(button)
基维语:
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
id:bxl2
这是一个简单的程序,但我找不到让它工作的方法。我只想在用户按下位于 boxlayout1 中的按钮(并且没有在 textinput 中写入任何内容)时在 boxlayout2 中添加一个小部件。小部件不显示在 screen.What 中,我应该怎么做?
main.py
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class BoxLayout1(BoxLayout):
def Search(self):
if self.ids.textinput.text!='':
BoxLayout2()
class BoxLayout2(BoxLayout):
def Print(self):
self.add_widget(Button(text='hello'))
class TestApp(App):
pass
TestApp().run()
这是我的 kivy 代码
test.kv
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
我看到了我想要的演示文稿布局,但找不到按钮。
为了清楚起见,让我们跟随您编写的应用程序的流程。
- 它创建了一个BoxLayout 并将BoxLayout1 和BoxLayout2 放入其中,第二个没有任何内容。当您点击写入时,应用程序会检查文本框的内容,如果有效,则调用 BoxLayout2 的构造函数!现在,它创建了这个 class 的一个实例,但不保留它的引用,所以它会立即被丢弃。现在你想要的是调用一个当前存在的实例的函数,而不是创建另一个实例。这是代码:
python:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class BoxLayout1(BoxLayout):
def Search(self):
if self.ids.textinput.text!='':
self.parent.ids.bxl2.addButton()
# BoxLayout2()
class BoxLayout2(BoxLayout):
def addButton(self):
button=Button(text='hello')
self.add_widget(button)
基维语:
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
id:bxl2