以下是一个使用Tkinter包实现的图形界面程序,实现了你所描述的功能:

import tkinter as tk
from tkinter import filedialog, scrolledtext

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        # 文件浏览框
        self.file_path = tk.StringVar()
        self.file_label = tk.Label(self, textvariable=self.file_path)
        self.file_label.pack()

        self.browse_button = tk.Button(self, text="选择文件", command=self.open_file)
        self.browse_button.pack()

        # 提交按钮
        self.submit_button = tk.Button(self, text="提交", command=self.submit)
        self.submit_button.pack()

        # 完成度滚动条
        self.progressbar = tk.Scrollbar(self, orient="horizontal")
        self.progressbar.pack(fill="x")

        # 动态文本显示框
        self.text_area = scrolledtext.ScrolledText(self, wrap=tk.WORD, xscrollcommand=self.progressbar.set)
        self.text_area.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        self.progressbar.config(command=self.text_area.xview)

    def open_file(self):
        file_path = filedialog.askopenfilename()
        self.file_path.set(file_path)

    def submit(self):
        file_path = self.file_path.get()
        if file_path:
            with open(file_path, "r") as file:
                lines = file.readlines()
                total_lines = len(lines)

                for i, line in enumerate(lines):
                    line_number = i + 1
                    character_count = len(line.strip())
                    message = f"第{line_number}行:字符个数:{character_count}\n"
                    self.text_area.insert(tk.END, message)
                    self.text_area.see(tk.END)  # 滚动到最新内容

                    # 更新滚动条位置
                    completion = (line_number / total_lines) * 100
                    self.progressbar.set(completion)

root = tk.Tk()
app = Application(master=root)
app.mainloop()

使用这段代码,你可以运行一个图形界面程序,其中包含一个文件浏览框、提交按钮、完成度滚动条和动态文本显示框。你可以通过文件浏览框选择一个文本文件,然后点击提交按钮,程序将解读文件内容并在动态文本显示框中显示解读结果,同时滚动条将显示解读文件的完成度。动态文本显示框带有水平和垂直方向的滚动条,可以自动滚动显示文件每行的字符个数,随着解读文件的进行,会实时显示最新内容。

请注意,该程序使用了tkinter.filedialog模块中的askopenfilename函数来选择文本文件,因此需要将文本文件放在与该代码文件相同的目录下


原文地址: https://www.cveoy.top/t/topic/hK4I 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录