这是一个简单的程序,但我找不到让它工作的方法。我只是想在boxlayout2中添加一个小部件,当用户按下位于boxlayout1的按钮(并且没有在textinput中写入任何内容时)。小部件不会显示在屏幕上。我该怎么办?
main.py
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
这是我的kivy代码
test.kv
X = np.random.randn(4000,270)
y = np.ones((4000,1))
y[0:999] = 2
y[1000:1999] = 3
y[2000:2999] = 0
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
num_classes = 4
X_train = X_train.reshape(X_train.shape[0], 45, 6).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 45, 6).astype('float32')
model = Sequential()
model.add(Conv1D(filters=32, kernel_size=5, input_shape=(45, 6)))
model.add(MaxPooling1D(pool_size=5 ))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
我看到了我想要的演示文稿布局,但按钮无处可寻。
答案 0 :(得分:0)
为了清楚说明,请按照您撰写的应用流程进行操作。
蟒:
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)
kivy语言:
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
id:bxl2