Ответ 1
Вопрос будет лучше принят, если вы предоставили какой-либо код, который вы пытались написать для части GUI вашего вопроса. Я знаю (как и все, кто разместил на ваших комментариях), что tkinter хорошо документирован и имеет бесчисленные сайты и видеоролики YouTube.
Однако, если вы пытались написать код с помощью tkinter и просто не понимаете, что происходит, я написал небольшой базовый пример того, как написать графический интерфейс, который откроет файл и распечатает каждую строку на консоли.
Это не поможет вам ответить на ваш вопрос, но укажет вам в правильном направлении.
Это версия, отличная от OOP, которая, судя по существующему коду, вы можете лучше понять.
# importing tkinter as tk to prevent any overlap with built in methods.
import tkinter as tk
# filedialog is used in this case to save the file path selected by the user.
from tkinter import filedialog
root = tk.Tk()
file_path = ""
def open_and_prep():
# global is needed to interact with variables in the global name space
global file_path
# askopefilename is used to retrieve the file path and file name.
file_path = filedialog.askopenfilename()
def process_open_file():
global file_path
# do what you want with the file here.
if file_path != "":
# opens file from file path and prints each line.
with open(file_path,"r") as testr:
for line in testr:
print (line)
# create Button that link to methods used to get file path.
tk.Button(root, text="Open file", command=open_and_prep).pack()
# create Button that link to methods used to process said file.
tk.Button(root, text="Print Content", command=process_open_file).pack()
root.mainloop()
В этом примере вы сможете выяснить, как открыть файл и обработать его в графическом интерфейсе tkinter.
Для более опций OOP:
import tkinter as tk
from tkinter import filedialog
# this class is an instance of a Frame. It is not required to do it this way.
# this is just my preferred method.
class ReadFile(tk.Frame):
def __init__(self):
tk.Frame.__init__(self)
# we need to make sure that this instance of tk.Frame is visible.
self.pack()
# create Button that link to methods used to get file path.
tk.Button(self, text="Open file", command=self.open_and_prep).pack()
# create Button that link to methods used to process said file.
tk.Button(self, text="Print Content", command=self.process_open_file).pack()
def open_and_prep(self):
# askopefilename is used to retrieve the file path and file name.
self.file_path = filedialog.askopenfilename()
def process_open_file(self):
# do what you want with the file here.
if self.file_path != "":
# opens file from file path and prints each line.
with open(self.file_path,"r") as testr:
for line in testr:
print (line)
if __name__ == "__main__":
# tkinter requires one use of Tk() to start GUI
root = tk.Tk()
TestApp = ReadFile()
# tkinter requires one use of mainloop() to manage the loop and updates of the GUI
root.mainloop()