简体   繁体   English

在PysimpleGUI的输入法中输入文字后,如何自动将光标置于输入文字的末尾?

[英]After entering a text in the input of PysimpleGUI, how do I put the cursor to the end of the input text automatically?

I have created a pysimplegui window, like the following:我创建了一个 pysimplegui 窗口,如下所示:

Now, as I input a text, the input text is shown as the following:现在,当我输入文本时,输入文本显示如下:

But I want the cursor to be located to the right of the text automatically as the text is entered, like the following:但我希望光标在输入文本时自动位于文本的右侧,如下所示: 在此处输入图片说明

This is my code for the window:这是我的窗口代码:

# 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')])] ]

How do I do that?我怎么做?

There's no existing method to view/move cursor to end of sg.Input .没有现有的方法可以查看/移动光标到sg.Input

  • Call tkinter method xview_moveto(f) to positions the text in the sg.Input , the f argument must be in the range [0,1], where 0 means the left end of the text and 1 the right end.调用xview_moveto(f)方法xview_moveto(f)将文本定位在sg.Input ,f 参数必须在 [0,1] 范围内,其中 0 表示文本的左端,1 表示右端。
  • Call tkinter method icursor(index) to set the insertion cursor just before the character at the given index, end` for end of line.调用icursor(index) to set the insertion cursor just before the character at the given index,方法icursor(index) to set the insertion cursor just before the character at the given index, end` 为行尾。

Example Code示例代码

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()

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

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