禁用按钮,直到文本输入框中输入文本 – PySimpleGUI

huangapple go评论51阅读模式
英文:

Disabling button while TextInputs have not had text input into them - PySimpleGUI

问题

我正在使用PySimpleGui编写一个简单的GUI脚本,并且已经尝试了一个星期来解决这个问题,但是无论如何搜索或尝试各种解决方案似乎都不能达到我想要的效果。

GUI很简单,它有两个sg.InputText输入框和一个sg.Multiline输入框。还有一个sg.Button,按下时运行一个函数,该函数以这3个输入的值作为其参数。我希望在用户没有在输入框中输入任何文本时将按钮禁用,并在他们输入后重新启用。这将防止用户在输入所需内容之前运行工具。我尝试了基本的逻辑,但似乎无法使其工作。

示例代码:

import PySimpleGUI as sg

inputs_column = [
    [sg.Text(text='Enter value 1'), sg.InputText()],

    [sg.Text(text='Enter value 2'), sg.InputText()],

    [sg.Text(text='Enter values 3')],
    [sg.Multiline(size=(60, 20), sbar_background_color="grey42", key="value_list")],

    [sg.Button('Start', disabled=True), sg.Button('Quit')]
]

layout = [[sg.Column(inputs_column)]]

# 创建窗口
window = sg.Window('Fancy Doo-hickey', layout)

# 事件循环以处理“事件”并获取输入的“值”
while True:
    event, values = window.read()

    # 如果用户关闭窗口或点击取消,则终止进程
    if event == sg.WIN_CLOSED or event == 'Quit':
        break

    # 当按下“Start”按钮时要执行的操作
    if event == "Start":
        # 将用户输入的值转换为列表
        value_list = values['value_list'].strip().splitlines()
        
        # 检查用户是否已输入内容
        while values[0] == '' and values[1] == '' and len(value_list) == 0:
            window['Start'].update(disabled=True)
        else:
            window['Start'].update(disabled=False)

        # 运行主要函数
        run_function(arg1=values[0], arg2=values[1], arg3=value_list)

有任何想法吗?PySimpleGUI是否能够在事件触发之前监视TextInput框中输入的内容?感谢您的帮助。

英文:

I am writing a simple GUI script using PySimpleGui and have been trying for a week to figure this out, but no amount of googling or trying various solutions seems to do what I want.

The gui is simple, it has two sg.InputText inputs, and one sg.Mulitline input. There is also an sg.Button that kicks off running a function, that takes the values of the 3 inputs as its arguments, when pressed. I would like to have the button be disabled out while the user has not entered any text into the input boxes, and re-enabled once they have. This will keep the user from being able to run the tool before they enter the required inputs. I have tried basic logic, but cant seem to get it to work.

Sample Code:

import PySimpleGUI as sg

inputs_column = [
    [sg.Text(text='Enter value 1'), sg.InputText()],

    [sg.Text(text='Enter value 2'), sg.InputText()],

    [sg.Text(text='Enter values 3')],
    [sg.Multiline(size=(60, 20), sbar_background_color="grey42", key="value_list")],

    [sg.Button('Start', disabled=True), sg.Button('Quit')]
]

layout = [[sg.Column(inputs_column)]]

# Create the Window
window = sg.Window('Fancy Doo-hickey', layout)

# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()

    # kill the process if user closes window or clicks cancel
    if event == sg.WIN_CLOSED or event == 'Quit':
        break

    # what to do when the start button is pressed
    if event == "Start":
        # turn the values given by the user input into a list
        value_list = values['value_list'].strip().splitlines()
        
        # check to see if user has given input yet
        while values[0] == '' and values[1] == '' and len(value_list) == 0:
            window['Start'].update(disabled=True)
        else:
            window['Start'].update(disabled=False)

        # run the main function
        run_function(arg1=values[0], arg2=values[1], arg3=value_list)

Any ideas? Is it even possible for PySimpleGUI to monitor what is entered into the TextInput boxes before an event is triggered?

Any help is much appreciated.

答案1

得分: 1

以下是您要翻译的内容:

"需要在元素内容更改时生成事件,因此添加一个名为 enable_events=True 的选项,并处理该事件以决定是否禁用按钮。

简化后的代码如下。

import PySimpleGUI as sg

def func(*args):
        print(*tuple(map(repr, args)))

layout = [
    [sg.Input(enable_events=True, key='IN 1')],
    [sg.Input(enable_events=True, key='IN 2')],
    [sg.Multiline(size=(40, 5), enable_events=True, expand_x=True, key='IN 3')],
    [sg.Push(), sg.Button('Start', disabled=True)],
]
window = sg.Window('Demo', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event in ('IN 1', 'IN 2', 'IN 3'):
        disabled = not (values['IN 1'] and values['IN 2'] and values['IN 3'])
        window['Start'].update(disabled=disabled)

    elif event == 'Start':
        func(values['IN 1'], values['IN 2'], values['IN 3'])

window.close()
英文:

Need to generate an event when the content of element changed, so add one more option enable_events=True, and handle that event to decide if disable the button or not.

Reduced code as following.

import PySimpleGUI as sg

def func(*args):
        print(*tuple(map(repr, args)))

layout = [
    [sg.Input(enable_events=True, key='IN 1')],
    [sg.Input(enable_events=True, key='IN 2')],
    [sg.Multiline(size=(40, 5), enable_events=True, expand_x=True, key='IN 3')],
    [sg.Push(), sg.Button('Start', disabled=True)],
]
window = sg.Window('Demo', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event in ('IN 1', 'IN 2', 'IN 3'):
        disabled = not (values['IN 1'] and values['IN 2'] and values['IN 3'])
        window['Start'].update(disabled=disabled)

    elif event == 'Start':
        func(values['IN 1'], values['IN 2'], values['IN 3'])

window.close()

huangapple
  • 本文由 发表于 2023年6月13日 10:03:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定