我刚开始和Toga和Beeware合作(一直在学习他们的教程)来制作一个windows应用程序。我有一个函数,它通过excel文件并返回一个值列表。用户按下按钮来决定函数工作的时间框架。但是,当我启动应用程序时,我已经在文本框中得到了一个结果,而没有按下按钮。即使按下不同的按钮,结果也不会改变。这是代码:
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看起来非常直观)。谢谢。
发布于 2022-10-09 10:42:21
on_press=find_it(1)立即调用find_it,然后将其返回值(None)作为on_press处理程序传递。
相反,您应该像这样定义find_it:
def find_it(n):
def on_press(button):
'''does stuff with n'''
result_A.value = output
return on_presshttps://stackoverflow.com/questions/74003937
复制相似问题