Python >> Tutoriel Python >  >> Python GUI >> Tkinter GUI

Python 3 Tkinter Lire le fichier TXT et afficher le contenu du fichier TXT dans la fenêtre de canevas (Listbox) Widget GUI Desktop App

Python 3 Tkinter lit le fichier TXT et affiche le contenu du fichier TXT dans la fenêtre de canevas (Listbox) Widget GUI Desktop App

from tkinter import *
from tkinter import filedialog

def openFile():
    tf = filedialog.askopenfilename(
        initialdir="C:/Users/MainFrame/Desktop/", 
        title="Open Text file", 
        filetypes=(("Text Files", "*.txt"),)
        )
    pathh.insert(END, tf)
    tf = open(tf)  # or tf = open(tf, 'r')
    data = tf.read()
    txtarea.insert(END, data)
    tf.close()

ws = Tk()
ws.title("PythonGuides")
ws.geometry("400x450")
ws['bg']='#fb0'

txtarea = Text(ws, width=40, height=20)
txtarea.pack(pady=20)

pathh = Entry(ws)
pathh.pack(side=LEFT, expand=True, fill=X, padx=20)



Button(
    ws, 
    text="Open File", 
    command=openFile
    ).pack(side=RIGHT, expand=True, fill=X, padx=20)


ws.mainloop()