简体   繁体   English

停止语句执行直到为真

[英]Stopping Statement Execution Until true

I am trying to handle user input using pysimplegui .我正在尝试使用pysimplegui处理用户输入。

If the user enters any value in the input box I need to make sure it's an integer type.如果用户在输入框中输入任何值,我需要确保它是 integer 类型。

If successful, I need to replace user input value with default value for it.如果成功,我需要将用户输入值替换为默认值。

I tried to use try except within a while loop.我尝试while except内使用try My concern here is if the user enters a string value I need to give a retry option.我在这里担心的是,如果用户输入了一个字符串值,我需要提供一个重试选项。

However, the if else block below the try block is getting executed.但是, try块下方的if else块正在执行。 How can I make sure to give the option for the user to retry entering a value?如何确保为用户提供重试输入值的选项?

import PySimpleGUI as sg

sg.theme('SandyBeach')

layout = [
    [sg.Text('Please enter your Phone')],
    [sg.Text('Phone', size=(15, 1)), sg.InputText(key="lp1", do_not_clear=False)],
    [sg.Button("e"), sg.Quit()]
]
window = sg.Window('Simple data entry window', layout)
while True:

    event, values = window.read()

    if event in (sg.WINDOW_CLOSED, "Quit"):
        break
    elif event == "e":

        text = values['lp1']
        log_timer = 1200
        user_input = None

        if text == '':
            log_timer = 1200
        else:
            while True:
                try:
                    user_input = int(text)
                    break

                except ValueError:
                    sg.popup_error("Only Integer Allowed")
                    break

I need to stop the below block if a string value is entered.如果输入了字符串值,我需要停止下面的块。

        if type(user_input) == int:

            log_timer = user_input
            print(log_timer)

        elif log_timer == 1200:
            print("no cha")

window.close()
 

There is a lot of noise in your code.您的代码中有很多噪音。 When you reduce it to only what you need it becomes a lot clearer.当您将其减少到仅需要的内容时,它会变得更加清晰。

import PySimpleGUI as sg
sg.theme('SandyBeach')

layout = [
    [sg.Text('Please enter your Phone')],
    [sg.Text('Phone', size=(15, 1)), sg.InputText(key="lp1", do_not_clear=False)],
    [sg.Button("e"), sg.Quit()]
]
window = sg.Window('Simple data entry window', layout)
# Add some defaults for user_input and log_timer
user_input = None
log_timer = 1200
while True:
    event, values = window.read()
    text = values['lp1']
    # break on Quit
    if event in (sg.WINDOW_CLOSED, "Quit"):
        break
    elif event == "e" and text != '':
        # Attempt to convert to int
        try:
            user_input = int(text)
        except ValueError:
            sg.popup_error("Only Integers Allowed")
        # If the attempt was unsuccessful user_input will remain None
        if user_input is not None:
            # the only place log_timer is changed from the default
            log_timer = user_input
            break
print(log_timer)
window.close()

The two break s in your code mean it will only stop running on 2 conditions.代码中的两个break意味着它只会在 2 个条件下停止运行。 A valid number is entered.输入了一个有效数字。 Or the quit button was pressed.或者按下了退出按钮。

Having one place that log_timer is reassigned means the only way it can change from the default is if a valid number is entered.在一个地方重新分配log_timer意味着它可以从默认值更改的唯一方法是输入有效数字。

There are obviously improvements to be made once you learn functions and so on.一旦你学习了函数等等,显然有待改进。 But this is a good starting point.但这是一个很好的起点。

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

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