首页 > 解决方案 > Python 在其他模块中更新 scrolledtxt

问题描述

我的代码分为 2 个模块。一个是子(GUI-tkinter)模块,另一个是主(主功能模块)。

子模块(GUI)

import main

import tkinter as tk
from tkinter import filedialog
from tkinter import scrolledtext


class GUI():
def __init__(self, master):
   
    # create Window and widgets ...
   
    self.bt_gen = tk.Button(master, text="Generation", command=lambda: self.runGenCode())
    self.bt_gen.grid(row=3, column=1, padx=5, pady=5, sticky='nsew')  # button : generation

def setScrollTxt(self, text):
    self.txt_status.insert(tk.INSERT, text)
    self.txt_status.see(tk.END)

def clearScrollTxt(self):
    self.txt_status.delete('1.0', tk.END)

def runGenCode(self):
    main(self, self.dir_input, self.dir_output)
    self.setScrollTxt("\nCode Generated Successfully!!!\n")



root = tk.Tk()                                                      # create window

app = GUI(root)                                                     # create object

root.mainloop()

主模块(主函数)

def main(self, input, output):

    # function : import data from excel

    self.setScrollTxt("1/4. Import Data : Done!\n")
 
    # function : pre-process data 

    self.setScrollTxt("2/4. Data Pre-Process : Done!\n")


    with open(filename_output + ".h", "w") as fo:                      

    # function : write file

    self.setScrollTxt("3/4. Export Data to .h file : Done!\n")


    with open(filename_output + ".c", "w") as fo:

    # function : write file
    
    self.setScrollTxt("4/4. Export Data to .c file : Done!\n")


if __name__ == "__main__":
    main()

这是我的代码(简单版)

我的问题是'main'函数中没有更新scrolledtxt。每一步完成后,在'main'模块中调用函数'setScrollTxt'。但 scrolledtxt 没有改变。只有在完成 'main' 模块并执行 'root.mainloop' 之后,才会更新 scrolledtxt。

我怎么解决这个问题?

标签: pythontkinter

解决方案


推荐阅读