我想弹出一个窗口,以确认用户确实要退出该应用程序。现在,当我尝试将命令绑定到两个按钮时,我可以仅直接在函数内部添加消除功能,而无需通过回调。没关系。 但是我只能通过回调而不是在函数内部 调用关闭例程。当我在此函数内绑定 quit_app()时,在打开弹出窗口时它将直接执行。为什么?它应该绑定而不是执行。
(Old script deleted.)
我对脚本进行了一些更新,并包含了一个最小的kv文件。它基本上可以工作(像以前一样),但是看起来有点奇怪。
UI-Test.py:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
# Kivy imports:
import kivy
from kivy.app import App
from kivy.uix import popup
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.uix.tabbedpanel import TabbedPanel
VersionString = "DRAFT"
AppName = 'UI-Test'
def CloseProgram(Message, Level):
print('Closing, level {} ({})'.format(Level, Message))
sys.exit()
def OnClosing(self):
print('Closing...')
# ToDo: call popup
def init():
print('Starting {} Version {}.'.format(AppName, VersionString))
print('You are using Python version: {}'.format(sys.version))
class TestApp(App):
title = AppName + ' ' + VersionString
def on_pause(self):
return True
def quit_app(self,btn):
CloseProgram('Normal Closing', 'Debug')
class Pop(BoxLayout):
def __init__(self, **kwargs):
super(Pop, self).__init__(**kwargs)
self.up()
def callback(instance):
if instance.id == 'quit':
TestApp.quit_app(TestApp, 1)
def up(self):
print('popup')
qbutton = Button(text='Quit', id='quit')
abutton = Button(text='Return to Program', id='return')
blayout = BoxLayout()
blayout.add_widget(qbutton)
blayout.add_widget(abutton)
self.popup = kivy.uix.popup.Popup(title='Quit Program?', content=blayout, size_hint=(None, None), size=(400, 400))
abutton.bind(on_release=self.popup.dismiss)
qbutton.bind(on_release=TestApp.Pop.callback)
self.popup.open()
if __name__ == '__main__':
init()
TestApp().run()
Test.kv:
#:kivy 1.9
<Button>:
font_size: 15
# Main Layout:
BoxLayout:
orientation: 'vertical'
Button:
text: "Quit"
id: "quit_button"
size_hint: (0.1, None)
size: (150, 50)
on_release: app.Pop.up(self)
答案 0 :(得分:0)
您如何从kv文件中调用此弹出式窗口?在我的版本中(请参见 更新的脚本)Pop不是TestApp的一部分,我无法从kv访问它 文件
#:import Factory kivy.factory.Factory
<Pop>:
并添加小部件。class Pop()
注册,实例化并打开Factory.Pop().open()
工厂可用于自动注册任何类或模块 并在项目中的任何地方从中实例化类。
App.get_running_app()
获取class TestApp()
的实例#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
# Kivy imports:
import kivy
kivy.require('1.11.0')
from kivy.app import App
from kivy.uix.popup import Popup
VersionString = "DRAFT"
AppName = 'UI-Test'
def CloseProgram(Message, Level):
print('Closing, level {} ({})'.format(Level, Message))
sys.exit()
def OnClosing(self):
print('Closing...')
# ToDo: call popup
def init():
print('Starting {} Version {}.'.format(AppName, VersionString))
print('You are using Python version: {}'.format(sys.version))
class Pop(Popup):
def callback(self, instance):
App.get_running_app().quit_app(instance)
class TestApp(App):
title = AppName + ' ' + VersionString
def on_pause(self):
return True
def quit_app(self, btn):
CloseProgram('Normal Closing', 'Debug')
if __name__ == '__main__':
init()
TestApp().run()
#:kivy 1.11.0
#:import Factory kivy.factory.Factory
<Pop>:
title: 'Quit Program?'
size_hint: None, None
size: 400, 400
BoxLayout:
Button:
text: 'Quit'
on_release:
root.dismiss()
root.callback(self)
Button:
text: 'Return to Program'
on_release: root.dismiss()
<Button>:
font_size: 15
# Main Layout:
BoxLayout:
orientation: 'vertical'
Button:
text: "Quit"
id: "quit_button"
size_hint: (0.1, None)
size: (150, 50)
on_release: Factory.Pop().open()
Popup.open()
之前先绑定所有按钮 def callback(self, instance):
print("\ncallback:")
self.popup.dismiss()
App.get_running_app().quit_app(1)
def up(self):
...
self.popup = Popup(title='Quit Program?', content=blayout, size_hint=(None, None), size=(400, 400))
abutton.bind(on_release=self.popup.dismiss)
qbutton.bind(on_release=self.callback)
self.popup.open()
...
class aempApp(App):
...
def quit_app(self, value):
print(value)
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
class Pop(BoxLayout):
def __init__(self, **kwargs):
super(Pop, self).__init__(**kwargs)
self.up()
def callback(self, instance):
print("\ncallback:")
self.popup.dismiss()
App.get_running_app().quit_app(1)
def up(self):
print('popup')
qbutton = Button(text='Quit', id='quit')
abutton = Button(text='Return to Program', id='return')
blayout = BoxLayout()
blayout.add_widget(qbutton)
blayout.add_widget(abutton)
self.popup = Popup(title='Quit Program?', content=blayout, size_hint=(None, None), size=(400, 400))
abutton.bind(on_release=self.popup.dismiss)
qbutton.bind(on_release=self.callback)
self.popup.open()
class TestApp(App):
def build(self):
return Pop()
def quit_app(self, value):
print("value=", value)
if __name__ == "__main__":
TestApp().run()