python tkinter | 如何使得entry文本框靠右显示,从右向左填充,显示文本末尾

发布时间 2023-10-31 11:30:38作者: IssacNew
from tkinter import *
from tkinter import filedialog


app = Tk()
app.title("ABC")
app.geometry("300x100")

def browse_for_file(entry_name, filetype):
    File_path = filedialog.askopenfilename(filetypes=[("Excel Files", "*.xlsx")])
    entry_name.delete(0, END)     
    entry_name.insert(0, File_path)
    # 最关键的步骤就是使用xview_moveto(1) ,xview_moveto(0)表示显示左侧内容,xview_moveto(1)表示显示文本末尾内容
    # 特别需要注意的是,需要先插入内容,再使用xview_moveto。
    # 如果将entry_name.xview_moveto(1)  放在entry_name.insert(0, File_path)之前那么无法生效!
    entry_name.xview_moveto(1)   

templ_filename = StringVar()
templ_entry = Entry(app, textvariable = templ_filename, width = 30,justify="right")
templ_entry.xview_moveto(1)
templ_entry.grid(row = 3, column = 1, sticky=W)


filetype_fasta = [('fasta files', '*.fasta'), ('All files', '*.*')]
button_templ = Button(app, text = 'Browse', width =6, command = lambda:browse_for_file(templ_entry, filetype_fasta))
button_templ.grid(row = 3, column = 2)

app.mainloop()