首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Toga/Beeware按钮不能正常工作,代码返回输出而不按下按钮

Toga/Beeware按钮不能正常工作,代码返回输出而不按下按钮
EN

Stack Overflow用户
提问于 2022-10-09 10:23:52
回答 1查看 28关注 0票数 1

我刚开始和Toga和Beeware合作(一直在学习他们的教程)来制作一个windows应用程序。我有一个函数,它通过excel文件并返回一个值列表。用户按下按钮来决定函数工作的时间框架。但是,当我启动应用程序时,我已经在文本框中得到了一个结果,而没有按下按钮。即使按下不同的按钮,结果也不会改变。这是代码:

代码语言:javascript
复制
    import toga
    from toga.style import Pack
    from toga.style.pack import COLUMN, ROW, LEFT, RIGHT, Pack


class MyFunction(toga.App):

    def startup(self):
        main_box = toga.Box()
        top_box = toga.Box()
        bot_box = toga.Box()
        result = toga.Box()

        result_A = toga.TextInput(readonly=True)
        result_L = toga.Label('This is your output: ', style=Pack(text_align=RIGHT))

        def find_it(int):
            '''does stuff'''
            result_A.value = output


        button0 = toga.Button('hr1', on_press=find_it(1), style=Pack(padding=5))
        button1 = toga.Button('hr2', on_press=find_it(2), style=Pack(padding=5))
        button2 = toga.Button('hr3', on_press=find_it(3), style=Pack(padding=5))
        button3 = toga.Button('hr4', on_press=find_it(4), style=Pack(padding=5))
        button4 = toga.Button('hr5', on_press=find_it(5), style=Pack(padding=5))
        button5 = toga.Button('hr6', on_press=find_it(6), style=Pack(padding=5))
        button6 = toga.Button('hr7', on_press=find_it(7), style=Pack(padding=5))
        button7 = toga.Button('hr8', on_press=find_it(8), style=Pack(padding=5))
        button8 = toga.Button('hr9', on_press=find_it(9), style=Pack(padding=5))
        button9 = toga.Button('hr10', on_press=find_it(10), style=Pack(padding=5))


        top_box.add(button0)
        top_box.add(button1)
        top_box.add(button2)
        top_box.add(button3)
        top_box.add(button4)
        bot_box.add(button5)
        bot_box.add(button6)
        bot_box.add(button7)
        bot_box.add(button8)
        bot_box.add(button9)

        result.add(result_L)
        result.add(result_A)

        main_box.add(top_box)
        main_box.add(bot_box)
        main_box.add(result)

        main_box.style.update(direction=COLUMN, padding_top=10)
        top_box.style.update(direction=ROW, padding=5)
        bot_box.style.update(direction=ROW, padding=5)
        result.style.update(direction=ROW, padding=10)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()


def main():
    return MyFunction()

我认为问题在"on_press=find_it()“部分,因为输出总是与定义的最后一个按钮相关(所以button9,意味着find_it总是使用10作为参数)。我尝试更改find_it函数中的参数,按照不同的顺序添加"self,widget,int“;我还尝试了外接程序”self“。关于"on_press=“的论点,没有成功。我对Toga和所有的东西都很陌生,但是文档非常有限,我不能准确地指出我做错了什么。如果有人能告诉我一些广泛的文档,这样我也可以自己学习,我会很高兴的。或者甚至告诉我一个与Beeware兼容的不同的GUI框架,但是更多的文档和/或易用性(尽管Toga看起来非常直观)。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-09 10:42:21

on_press=find_it(1)立即调用find_it,然后将其返回值(None)作为on_press处理程序传递。

相反,您应该像这样定义find_it

代码语言:javascript
复制
def find_it(n):
   def on_press(button):
        '''does stuff with n'''
        result_A.value = output       
   return on_press
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74003937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档