【发布时间】:2018-04-27 09:22:44
【问题描述】:
如何使用回车键关闭popup.dismiss()。
当我在单击ok 按钮后运行test.py 然后打开一个弹出窗口。
有人可以告诉我如何使用回车键关闭popup 并在关闭弹出窗口后将focus=True 设置为name TextInput。
test.py
import kivy
kivy.require('1.9.0') # replace with your current kivy version !
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
Window.size = (500, 150)
class TestScreen(Screen):
def insert_data(self):
Alert(title='yeah!', text='inputs are invalid')
class Alert(Popup):
def __init__(self, title, text):
super(Alert, self).__init__()
box = BoxLayout(orientation='vertical', padding=(5))
box.add_widget(Label(text=text))
btn1 = Button(text="Close")
box.add_widget(btn1)
popup = Popup(title=title, title_size=(30),
title_align='center', content=box,
size_hint=(None, None), size=(300, 200),
auto_dismiss=True)
btn1.bind(on_press=popup.dismiss)
popup.open()
class Test(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
Test().run()
test.kv
TestScreen:
GridLayout:
cols: 2
padding: 30, 30
spacing: 10, 10
row_default_height: '30dp'
Label:
text: 'Name'
text_size: self.size
valign: 'middle'
#padding_x: 50
TextInput:
id: name
Button:
text: 'Ok'
on_press: root.insert_data()
Button:
text: 'Cancel'
on_press: app.stop()
【问题讨论】:
标签: python-2.7 kivy kivy-language