简体   繁体   English

Python Kivy 2 按钮 2 相同布局中的不同操作

[英]Python Kivy 2 Buttons 2 Different Actions in Same Layout

I develop voice assistant with python and ı want to add basic gui with kivyy.我用 python 开发语音助手,我想用 kivyy 添加基本的 gui。 I am writing basic gui with kivy and ı want to learn kivy framework so that ı am trying to improve myself with creating GUIs.我正在用 kivy 编写基本的 gui,我想学习 kivy 框架,以便我试图通过创建 GUI 来提高自己。 But in this project ı have an error.但是在这个项目中我有一个错误。 Firstly, let me put my code here.首先,让我把我的代码放在这里。

class Assistant(App):

def build(self):
    rl = RelativeLayout()



    b1 = Button(size_hint=(.2, .2),
                pos_hint={'x': 0, 'top': 1},
                text="Click For Exit!",
                color=(1,0, .15, 1))

    b2 = Button(size_hint=(.2, .2),
                pos_hint={'right': 1, 'top': 1},
                text="Run Program!",
                color=(8,1, .56, 4))


    image = Image(source="background_img.jpg")
    b2.bind(on_press=self.RunProgram)
    b1.bind(on_press=self.CloseProgram)


    textinput = TextInput(font_size=30,
                          size_hint_y=None,
                          height=100,
                          multiline=False,
                          allow_copy=True,
                          auto_indent=True)

    textinput.bind(text=self.on_text)
    textinput.bind(on_text_validate=self.on_enter)

    rl.add_widget(image)
    rl.add_widget(b1)
    rl.add_widget(textinput)
    rl.add_widget(b2)

and for this two button there is their functions.这两个按钮有它们的功能。

    def CloseProgram(self, event):
        sys.exit()

    def RunProgram(self, event): 
        while True:
              jarvis.run(listen())

As you can see ı have 2 button, one is quit from program and the other one is listen users.如您所见,我有 2 个按钮,一个是退出程序,另一个是监听用户。 Also ı have text input that user can input something to give command from keyboard to assistant.我也有文本输入,用户可以输入一些东西来从键盘向助手发出命令。 But unfortunately when ı click "Run Program" button, Everything is good at first after when ı want to type something or if ı want to click "Exit Program" my GUI stop working.但不幸的是,当我点击“运行程序”按钮时,当我想输入一些东西或者如果我想点击“退出程序”我的 GUI 停止工作之后,一切都很好。 ( everything freezes and crashes ) but in the background my assistant continues to listen to user (me). (一切都冻结和崩溃)但在后台我的助手继续听用户(我)。 How can ı solve this problem?我怎么解决这个问题?

İn this situation when ı delete while loop everything goes fine but when user wants to talk for assistant have to click the "Run Program" button every time.在这种情况下,当我删除 while 循环时一切正常,但是当用户想要与助手交谈时,每次都必须单击“运行程序”按钮。 Here ı want user can click buttons or can type to text input when program working on background.在这里我希望用户可以单击按钮或可以在程序在后台运行时键入文本输入。 Thanks in advance.提前致谢。

More information:更多信息:

I use windows10, and python version 3.8.1我用的是windows10,python版本3.8.1

The problem is solved using python thread .该问题使用 python线程解决。 Thanks everyone.感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM