Как открыть текстовый файл, используя filedialog.askopenfile в нашем графическом интерфейсе Python - PullRequest
0 голосов
/ 27 июня 2018
    #function to open the file
    import tkinter as tk
    from tkinter.scrolledtext import ScrolledText
    from tkinter import filedialog
    root = tk.Tk()
    textPad = ScrolledText(root)
        def open_command():
            file = filedialog.askopenfile(parent=root,mode='rt',title='Select a file')
            if file != None:
                contents = file.read()
                textPad.insert('1.0',contents)
                file.close()

Я хочу знать, как мне вставить содержимое, которое я прочитал, в мой GUI, чего не происходит.

1 Ответ

0 голосов
/ 27 июня 2018

Вы разместили свой прокручиваемый текст в графическом интерфейсе? Я добавил вызовы pack() и open_command(), и ваш код работал на меня.

import tkinter as tk
from tkinter.scrolledtext import ScrolledText
from tkinter import filedialog


root = tk.Tk()
textPad = ScrolledText(root)
textPad.pack()


def open_command():
    file = filedialog.askopenfile(parent=root,mode='rt',title='Select a file')
    if file != None:
        contents = file.read()
        textPad.insert('1.0',contents)
        file.close()

open_command()
root.mainloop()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...