【问题标题】:After entering a text in the input of PysimpleGUI, how do I put the cursor to the end of the input text automatically?在 PysimpleGUI 的输入中输入文本后,如何自动将光标放在输入文本的末尾?
【发布时间】:2021-12-06 16:24:10
【问题描述】:

我创建了一个 pysimplegui 窗口,如下所示:

现在,当我输入一个文本时,输入的文本如下所示:

但我希望光标在输入文本时自动位于文本右侧,如下所示:

这是我的窗口代码:

# Creating the GUI window.

sg.theme('DarkAmber')   # Add a touch of color

# All the stuff inside your window.
lengthOfInputBar = 40   # Length of the bar where inputs are entered.
layout = [ [sg.Text(text='Valgrind Message .txt File Location'), 
            sg.Input(default_text='', size=(lengthOfInputBar, 1), 
                     enable_events=True, key='-VALGRIND_OUTPUT_TXT_INP-'), 
            sg.FileBrowse(file_types=[('TXT (*.txt)', '*.txt')])] ]

我该怎么做?

【问题讨论】:

    标签: python input window pysimplegui


    【解决方案1】:

    没有现有的方法可以查看/移动光标到sg.Input 的末尾。

    • 调用tkinter方法xview_moveto(f)定位sg.Input中的文本,f参数必须在[0,1]范围内,其中0表示文本的左端,1表示右端。李>
    • 为行尾调用 tkinter 方法 icursor(index) to set the insertion cursor just before the character at the given index, end`。

    示例代码

    import PySimpleGUI as sg
    
    
    sg.theme('DarkAmber')
    
    lengthOfInputBar = 40
    layout = [
        [sg.Text(text='Valgrind Message .txt File Location'),
         sg.Input(default_text='', size=(lengthOfInputBar, 1), enable_events=True, key='-VALGRIND_OUTPUT_TXT_INP-'),
         sg.FileBrowse(file_types=[('TXT (*.txt)', '*.txt')])],
    ]
    window = sg.Window('Songs list', layout, finalize=True)
    entry = window['-VALGRIND_OUTPUT_TXT_INP-'].Widget
    
    while True:
    
        event, values = window.read()
    
        if event == sg.WIN_CLOSED:
            break
        elif event == '-VALGRIND_OUTPUT_TXT_INP-':
            entry.xview_moveto(1)       # View end
            entry.icursor('end')        # Move cursor to end
    
    window.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 2021-10-06
      • 2011-11-08
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      相关资源
      最近更新 更多