open a txt file and show contents using tkinter

Solutions on MaxInterview for open a txt file and show contents using tkinter by the best coders in the world

showing results for - "open a txt file and show contents using tkinter"
Jona
26 Oct 2017
1import tkinter as tk
2from tkinter import ttk
3from tkinter import filedialog as fd
4
5# Root window
6root = tk.Tk()
7root.title('Display a Text File')
8root.resizable(False, False)
9root.geometry('550x250')
10
11# Text editor
12text = tk.Text(root, height=12)
13text.grid(column=0, row=0, sticky='nsew')
14
15
16def open_text_file():
17    # file type
18    filetypes = (
19        ('text files', '*.txt'),
20        ('All files', '*.*')
21    )
22    # show the open file dialog
23    f = fd.askopenfile(filetypes=filetypes)
24    # read the text file and show its content on the Text
25    text.insert('1.0', f.readlines())
26
27
28# open file button
29open_button = ttk.Button(
30    root,
31    text='Open a File',
32    command=open_text_file
33)
34
35open_button.grid(column=0, row=1, sticky='w', padx=10, pady=10)
36
37
38root.mainloop()Code language: Python (python)
similar questions