发表文章

[Python] 在文本框中使用回调-如何? Using callback in textbox - how?[easygui]

fiddybux 2017-10-9 25

你好

下面的建议, 我一直在试图完善我的代码, 将命令传递到 easygui, 这主要有助于调试。一切都很顺利

但是, 我对文本框的回调函数有困难。

我读了下面的内容, 但在功能上没有实现它:

http://easygui.readthedocs.io/en/latest/api.html

这里说:

callback: function
if set, this function will be called when OK is pressed

None
If cancel is pressed

如果我有如下所示的回调集, 则它不按预期方式进行任何操作:

if easygui.ynbox(msg_a, title_a):
    pass
    easygui.textbox(
        msg=msg_b, title=title_b, text=ans_y_filecont, callback=None)
else:
    easygui.textbox(
        msg=msg_b, title=title_c, text=ans_n_filecont, callback=None)

如果我按如下方式编写代码, 只要在屏幕上显示文本框, 它就会退出 (在退出 () 命令之后), 这样文本框就会立即消失:

callback_quit = quit()
if easygui.ynbox(msg_a, title_a):
    pass
    easygui.textbox(
        msg=msg_b, title=title_b, text=ans_y_filecont, callback=callback_quit)
else:
    easygui.textbox(
        msg=msg_b, title=title_c, text=ans_n_filecont, callback=callback_quit)

至少我知道退出 () 工作, 但为什么不等待, 直到按下 "取消" 按钮的回调?

是否错误地实现了回调函数?

是否有另一个函数我应该调用回调?

谢谢.

原文:

Hi,

Following advice, I've been trying to refine my code to pass commands into easygui, which primarily helps with debugging. It's been going well, on balance.

However, I'm having difficulty with the callback function of the textbox.

I've read the following and I've failed to implement it functionally:

http://easygui.readthedocs.io/en/latest/api.html

Here is says that:

callback: function
if set, this function will be called when OK is pressed

None
If cancel is pressed

If I have callback set as follows, it does nothing, as expected:

if easygui.ynbox(msg_a, title_a):
    pass
    easygui.textbox(
        msg=msg_b, title=title_b, text=ans_y_filecont, callback=None)
else:
    easygui.textbox(
        msg=msg_b, title=title_c, text=ans_n_filecont, callback=None)

If I write my code as follows, it just quits (following issue of quit() command) as soon as the textbox is presented on screen, so the textbox immediately disappears:

callback_quit = quit()
if easygui.ynbox(msg_a, title_a):
    pass
    easygui.textbox(
        msg=msg_b, title=title_b, text=ans_y_filecont, callback=callback_quit)
else:
    easygui.textbox(
        msg=msg_b, title=title_c, text=ans_n_filecont, callback=callback_quit)

At least I know quit() is working, but why isn't it waiting until the callback on CANCEL button is pressed?

Am I implementing the callback function incorrectly?

Is there another function I should be calling with callback?

Thanks.

相关推荐
最新评论 (8)
zadacka 2017-10-9
1

您是否可以调试并确认代码实际上是全部执行的?

pass 在您的块中间, 您将无法到达创建第一个文本框的代码。如果这是您希望看到的文本框, 那么您的问题与回调无关。

callback_quit = quit()
if easygui.ynbox(msg_a, title_a):
    pass  # pass will prevent the below line ever being executed
    easygui.textbox(
        msg=msg_b, title=title_b, text=ans_y_filecont, callback=callback_quit)
else:
    easygui.textbox(
        msg=msg_b, title=title_c, text=ans_n_filecont, callback=callback_quit)

如果这不能解决问题, 您是否可以将代码示例减少到一个更小的部分, 以显示导致问题的单个 EasyGUI 调用?如果我可以 re-produce 它, 这也将是有益的-所以这将是很好的, 看看什么参数值和回调是你正在使用。

原文:

Could you debug and confirm that the code is actually all being executed?

From that pass in the middle of your block it will be impossible for you to ever reach the code where you create the first textbox. If that is the textbox you are expecting to see, then your problem is nothing to do with the callback.

callback_quit = quit()
if easygui.ynbox(msg_a, title_a):
    pass  # pass will prevent the below line ever being executed
    easygui.textbox(
        msg=msg_b, title=title_b, text=ans_y_filecont, callback=callback_quit)
else:
    easygui.textbox(
        msg=msg_b, title=title_c, text=ans_n_filecont, callback=callback_quit)

In the event that this does not fix the problem, could you reduce the code sample to a smaller segment showing exactly the single EasyGUI call that causes problems? If I can re-produce it that would also be helpful - so it would be good to see what the argument values and callback are that you're using.

fiddybux 2017-10-9
2

你好

删除 "pass" 命令无法解决此问题。我接受我应该一直在使用它;我包括了这个, 因为我完全误解了它的功能。我相信这将是一个 ynbox 的选择。我错了。

不管怎样, 搬家也没用

我已经重写了一些测试代码, 您应该能够复制和粘贴逐字。有两个例子:

import easygui

msg_conf = "Continue..."
title_choose = "Choose Y or N..."
msg_y = "You pressed Yes..."
title_y = "You're a Yes presser..."
msg_n = "You pressed No..."
title_n = "You're a No presser..."
text_y = "The rhythm is the bass"
text_n = "The bass is the treble"
if easygui.ynbox(msg=msg_conf, title=title_choose):
    easygui.textbox(
        msg=msg_y, title=title_y, text=text_y, callback=None)
else:
    easygui.textbox(
        msg=msg_n, title=title_n, text=text_n, callback=None)
msg_final = (
    "If you made it this far then perhaps it didn't exit on CANCEL button! "
    "Or you ended up here as a correct result of the OK button. "
    "Whether you press YES or CANCEL results in the same situation...here.")
title_final = "You made it! But was it by mistake or by design?"
easygui.textbox(msg=msg_final, title=title_final)


在这两种情况下, 我们都有回调 = 无, 无论是从代码的 if/其他部分中选择 Y 还是 N。

如果更换退出 ()None元素, 如下所:

import easygui

msg_conf = "Continue..."
title_choose = "Choose Y or N..."
msg_y = "You pressed Yes..."
title_y = "You're a Yes presser..."
msg_n = "You pressed No..."
title_n = "You're a No presser..."
text_y = "The rhythm is the bass"
text_n = "The bass is the treble"
if easygui.ynbox(msg=msg_conf, title=title_choose):
    easygui.textbox(
        msg=msg_y, title=title_y, text=text_y, callback=quit())
else:
    easygui.textbox(
        msg=msg_n, title=title_n, text=text_n, callback=quit())
msg_final = "None of this will print at all."
title_final = "You didn't make it this far! Sorry."
easygui.textbox(msg=msg_final, title=title_final)

它甚至没有机会显示 Y 或 N 的结果 (if/其他部分的代码), 我们没有看到我可爱的消息在年底。

我的理解是, 如果用户按下文本框上的 "取消" 按钮, 则回调选项应调用, 因此, "确定" 按钮将前进到下一阶段。这是预定的功能吗?

当然, 也有相当高的机会, 我已经做了一个烂摊子的东西, 但一个生活在希望, 我实际上是学习的东西, 并改善!

不管怎样, 回到你我想。

谢谢!

原文:

Hi,

Removing the 'pass' command did not fix the situation. I accept I should have been using it anyway; I included this because I totally misunderstood its function. I believed it would the YES option for a ynbox. I was wrong.

Anyway, removal didn't help.

I have re-written a bit of test code that you should be able to copy and paste verbatim. There are two examples:

import easygui

msg_conf = "Continue..."
title_choose = "Choose Y or N..."
msg_y = "You pressed Yes..."
title_y = "You're a Yes presser..."
msg_n = "You pressed No..."
title_n = "You're a No presser..."
text_y = "The rhythm is the bass"
text_n = "The bass is the treble"
if easygui.ynbox(msg=msg_conf, title=title_choose):
    easygui.textbox(
        msg=msg_y, title=title_y, text=text_y, callback=None)
else:
    easygui.textbox(
        msg=msg_n, title=title_n, text=text_n, callback=None)
msg_final = (
    "If you made it this far then perhaps it didn't exit on CANCEL button! "
    "Or you ended up here as a correct result of the OK button. "
    "Whether you press YES or CANCEL results in the same situation...here.")
title_final = "You made it! But was it by mistake or by design?"
easygui.textbox(msg=msg_final, title=title_final)


Here we have callback=None in both cases whether Y or N is selected from the if / else parts of the code.

If you swap-out the None element for quit(), like so:

import easygui

msg_conf = "Continue..."
title_choose = "Choose Y or N..."
msg_y = "You pressed Yes..."
title_y = "You're a Yes presser..."
msg_n = "You pressed No..."
title_n = "You're a No presser..."
text_y = "The rhythm is the bass"
text_n = "The bass is the treble"
if easygui.ynbox(msg=msg_conf, title=title_choose):
    easygui.textbox(
        msg=msg_y, title=title_y, text=text_y, callback=quit())
else:
    easygui.textbox(
        msg=msg_n, title=title_n, text=text_n, callback=quit())
msg_final = "None of this will print at all."
title_final = "You didn't make it this far! Sorry."
easygui.textbox(msg=msg_final, title=title_final)

...it doesn't even get the chance to display the result for Y or N (the if / else parts of the code), and we don't get to see my lovely message at the end.

It is my understanding that the callback option should invoke if the user presses the CANCEL button on the textbox, and thus, the OK button progresses to the next stage. Is this the intended function?

Of course, there's also a reasonably high chance that I've made a mess of something, but one lives in hope that I am actually learning something and improving!

Anyway, back to you I guess.

Thanks!

zadacka 2017-10-9
3

好吧, 看起来好像有几件事情在发生。

首先, 让我们把你的样品降到最低:

import easygui
easygui.textbox(msg='some mesage', title='some title', text='some text', callback=quit())

这是我之前真正要求的--说明问题所需的最少代码量。没有无关的是-没有框或如果逻辑-只是导致问题的函数调用。它使我的生活更容易到达根本原因更快:)

从运行上面的示例代码可以看出, 我们从不显示框。这是因为您实际上是调用, quit 而不是将其作为回调方法传递。因此, 当 Python 试图获取回调参数的值时, 它实际上退出了。

根据您的评论, 您在这里要做的是传递 quit 方法作为回调, 如下所说:

import easygui
easygui.textbox(msg='some mesage', title='some title', text='some text', callback=quit)

现在, 接下来要理解的是, 如果按下 "Ok" 按钮, 则不会调用用户回调, 如果按了 "取消", 则不会。当你从文档中引用时, 你的第一个评论就有了这个权利。我们可以通过引入打印语句来看到这一点:

import easygui

easygui.textbox(msg='some mesage', title='some title', text='some text', callback=quit)
print 'we reached this print statement'

现在, 如果您按 "取消", 则不调用回调, 并且打印语句将执行, 以便您在控制台中看到输出。如果按下 "Ok" 按钮, 则调用回调并退出, 因此您永远无法到达 print 语句。

这一切都有意义吗?

如果您对文档中的某些东西不确定, 应鼓励您去查看实际的源代码。文档的过期和/或有时可能会误导, 源代码实际上会告诉你发生了什么。在这种情况下, 源将显示使用回调的条件。

原文:

Okay, it looks like there are a few things going on.

First, let's cut your sample down to the minimum:

import easygui
easygui.textbox(msg='some mesage', title='some title', text='some text', callback=quit())

This is what I was really asking for before - the minimum amount of code required to illustrate the problem. No unrelated yes-no boxes or if logic - just the function call that is causing problems. It makes life easier for me to get to the root cause more quickly :)

As you can see from running the sample code above, we never display a box. This is because you are actually calling quit rather than passing it as a callback method. So when Python tries to get the value for the callback argument it actually exits.

Based on your comment, what you intend to do here is pass the quit method for use as a callback, like this:

import easygui
easygui.textbox(msg='some mesage', title='some title', text='some text', callback=quit)

Now the next thing to understand here is that the user callback gets called if the 'Ok' button is pressed, NOT if 'cancel' is pressed. You got this right in your first comment when you quoted from the docs. We can see this by introducing a print statement:

import easygui

easygui.textbox(msg='some mesage', title='some title', text='some text', callback=quit)
print 'we reached this print statement'

Now if you press 'cancel' the callback is NOT called, and the print statement will execute so you see output in the console. If the 'Ok' button is pressed then you call the callback and quit, and so you never reach the print statement.

Does that all make sense?

If you are ever unsure about something in the documentation, do be encouraged to go and look at the actual source code. Documentation gets out of date and/or can sometimes be misleading, and the source will actually tell you what is going on. In this case the source will show the conditions under which the callback is used.

fiddybux 2017-10-9
4

这有点道理, 但我不确定我的系统是否像你描述的那样做出反应。

例如, 关于以下代码:

import easygui

easygui.textbox(
    msg='some message', title='some title', text='some text', callback=quit)  
# quit will just end here on OK, CANCEL continues to next block

print("we reached this print statement")
easygui.textbox(
    msg='some message 2', title='some title 2', text='some text 2', callback=None)

在本例中, 我期望cancel的按钮推送以调用 "退出" 作为回调的一部分, 并在按下取消时, 我期望代码终止, 但这不是发生在我身上的事情。相反, 它正在移动到下一个代码块, 打印邮件, 并向我展示我创建的第二个文本框来测试它。

对于我来说, OK按钮正在终止代码, 并且可能会调用 "退出" 回调。我现在很困惑, 因为结果和我预想的和阅读的不同。不过, 我已经总结了您关于过期文档的观点, 我还没有时间来检查您所指出的来源。

这里的功能是回到前面吗?或者这就是它的运作方式?

谢谢.

原文:

It sort of make sense, but I'm not sure if my system is responding the same way as you described.

For example, regarding the following code:

import easygui

easygui.textbox(
    msg='some message', title='some title', text='some text', callback=quit)  
# quit will just end here on OK, CANCEL continues to next block

print("we reached this print statement")
easygui.textbox(
    msg='some message 2', title='some title 2', text='some text 2', callback=None)

In this example, I expected a button push of CANCEL to invoke 'quit' as part of the callback, and upon pressing CANCEL I expected the code to terminate, but this is not what is happening for me. Instead it is moving on to the next block of code, printing the message and presenting me with the second textbox I created to test it.

For me, the OK button is terminating the code, and presumably invoking the 'quit' callback. I'm quite confused now because the result is different to what I expected and read. However, I have taken stock of your point about outdated documentation, and I have not yet had to time to check the source as you indicate.

Is the functionality here back to front? Or is this the way it is meant to operate?

Thanks.

zadacka 2017-10-9
5

在这种情况下, 文档是正确的, 您的行为是预期的。

如文档所说 (您在开始时引用):
回调: 函数
如果设置, 则在按下 "确定" 时将调用此函数

正如我在前面的答复中所述:
如果按下 "确定" 按钮, 而不是按下 "取消" 键, 则调用用户回调

我不认为我能解释得比这更清楚。这里的回调函数旨在为您提供一种在用户按 "Ok" 时使额外的内容发生的方法。您已将其连接到 quit 方法。它的行为和描述的一模一样。

你可能只是太接近这个问题, 看看它-休息一下, 回到它, 也许?在我读到这篇文章的时候, 行为的描述和运作方式并没有任何矛盾。

原文:

In this case, the documentation is correct and your behaviour is expected.

Like the docs say (and you quoted at the start):
callback: function
if set, this function will be called when OK is pressed

And as in my previous reply above:
the user callback gets called if the 'Ok' button is pressed, NOT if 'cancel' is pressed

I don't think that I can explain it much clearer than that. The callback function here is intended to provide you with a way to make extra stuff happen when the user presses 'Ok'. You've wired it up to the quit method. It is behaving exactly as described.

You may be just too close to the problem to see it - take a break and come back to it, maybe? As I read it, there are not any contradictions in the way the behaviour is described and how it is operating.

fiddybux 2017-10-9
6
原文:
zadacka 2017-10-9
7

没问题啊!那最后一点有意义吗?我有时会太接近一个问题, 不得不退后一步, 看看发生了什么事, 所以也许你正在经历同样的事情?

我不是 EasyGUI 的创造者--最初的功劳是给斯蒂芬. 参考的。我只是想帮助维护这个项目, 偶尔帮帮忙。

原文:

No problem-o! Did that last bit make sense? I sometimes get too close to a problem and have to step back a bit to see what's going on, so maybe you're experiencing the same thing?

I'm not the creator of EasyGUI - original credit for that goes to Stephen Ferg. I'm just trying to help maintain the project and occasionally help out.

fiddybux 2017-10-9
8
原文:
返回
发表文章
fiddybux
文章数
1
评论数
4
注册排名
60823