继续之前如何让程序代码等待用户从GUI输入

继续之前如何让程序代码等待用户从GUI输入

本文介绍了继续之前如何让程序代码等待用户从GUI输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中有一个复杂的计算,需要用户评估中间结果。这在命令行应用程序(这是我的代码看起来像现在)中运行良好,因为交互式提示将暂停程序执行,直到用户进入。命令行代码看起来像这样:

  def calculate(starting):
result1 = initial_calculation(starting)
user_input1 = input(你的选择为+ result1 +?)
result2 = calculate1(user_input1)
user_input2 =输入(你的选择为+ result2 + ?)
result2 = calculate2(user_input2)
#... etc

我想通过使用GUI提供一个比命令行更直观的界面,并允许用户点击指示他们选择的按钮,而不是键入它。我正在绘制如下所示:


  def do_something(starting):
result1 = initial_calculation(starting)
#等待用户按下一个按钮他们的选择?
result2 = calculate1(clicked_button.user_input1)

label.text(你的选择为+ result1 +?)
#wait再次为
result2 = calculate2(clicked_button.user_input2)
#... etc

有没有方法我可以暂停执行计算的程序代码,然后在用户单击按钮后恢复代码?我不太确定如何提高或处理这里的事件,因为通常在GUI中,控制回调是代码执行的起点,但是在这里,代码从别处开始,需要从GUI恢复。



(如果有所作为,我使用的是Python和wxPython。)

解决方案

p>一般解决方案:

  flag = false; 

您的主题:

 while(!flag); //等等这里,消费cpu不做任何

gui线程:

  void OnInputEvent(){flag = true; } 


In my program there is a complex calculation that requires the user to evaluate intermediate results. This works well in a command line application (which is what my code looks like now) because the interactive prompt halts the program execution until the user hits enter. The command line code looks something like this:

def calculate(starting):
    result1 = initial_calculation(starting)
    user_input1 = input("What is your choice for " + result1 + "?")
    result2 = calculate1(user_input1)
    user_input2 = input("What is your choice for " + result2 + "?")
    result2 = calculate2(user_input2)
    #...etc

I would like to provide a more intuitive interface than is possible with the command line by using a GUI and allowing the user to click on a button indicating their choice instead of typing it in. I'm picturing something like this:

def do_something(starting):
    result1 = initial_calculation(starting)
    #wait for user to press a button indicating their choice?
    result2 = calculate1(clicked_button.user_input1)

    label.text("What is your choice for " + result1 + "?")
    #wait for user again
    result2 = calculate2(clicked_button.user_input2)
    #...etc

Is there a way I can pause the execution of the procedural code that does the calculations and then resume the code after the user clicks a button? I'm not really sure how to raise or handle events here because normally in a GUI the control callbacks are the starting point for code execution, but here, the code starts elsewhere and needs to yield to and resume from the GUI.

(If it makes a difference, I am using Python and wxPython.)

解决方案

General solution:

flag = false;

your thread:

while (!flag); // wait here, consume cpu doing nothing

gui thread:

void OnInputEvent() { flag = true; }

这篇关于继续之前如何让程序代码等待用户从GUI输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:39