如果按钮回调中有另一个文件中的函数,则我的kivy应用程序在按下按钮后关闭



按下按钮时调用的函数

def Button1_Callback(instance):
show_popup()
Greet.timex()
print(f"Button {instance.text} was pressed.")

弹出功能:

def show_popup():
layout = GridLayout(cols = 1, padding = 5)
label = Label(text="executing request")
layout.add_widget(label)
pop = Popup(title="Status", content = layout, size_hint = (None, None),  size=(200, 200), auto_dismiss=True)
pop.open()

以及Button1_Callback内部的功能

def timex():                                                                    # replies with current time
y = time.strftime("%I %M %P")
engine.say("The time is " + y + "M")
engine.runAndWait()

该函数在另一个名为Greet.py的文件中

当我运行此应用程序时,代码运行良好但当我点击按钮时,timex功能将执行,应用程序将自动停止

同样,如果我注释行Greet.timex()并运行代码,那么问题就不会发生只有当Button1_Callback内部有函数时才会发生

您必须在ui线程中运行该函数(当您按下按钮时,该函数不会从ui线程启动(,否则您的应用程序将停止工作试试看。

from android.runnable import run_on_ui_thread
def Button1_Callback(instance):
show_popup()
run_on_ui_thread(Greet.timex)() #here we are making new function that will run on ui thread .
print(f"Button {instance.text} was pressed.")

快乐编码

最新更新