从另一个窗口更新一个窗口的文本窗口小部件

时间:2019-09-21 02:12:18

标签: tkinter

我正在创建一个tkinter应用程序,并希望向该应用程序添加一个新窗口,我可以在其中更新当前操作的状态。我的意图是,每次事件完成时,我都希望将其更新为并行运行的单独窗口的文本小部件。与控制台窗口类似,用户可以轻松阅读和了解应用程序/错误等的状态。

我对python和Tkinter还是很陌生,我的方法如下,

在直接将其包含在实际应用程序中之前,我试图在一个单独的项目中复制我的想法。

首先,我创建了一个简单的主窗口,如下所示,

from tkinter import *
from tkinter import ttk
from Console import ConsoleWindow

class Main:

    def __init__(self, master):

        self.master = master
        self.master.title('Main')
        self.master.geometry('640x480')
        self.btn = ttk.Button(self.master, text = 'Run Console', command = self.runConsole)
        self.btn.pack(side = LEFT)
        self.btn2 = ttk.Button(self.master, text = 'Update Console', command = self.updateConsole, state = DISABLED)
        self.btn2.pack(side = RIGHT)

    def runConsole(self):

        self.btn.configure(state = 'disabled')
        self.btn2.configure(state = 'normal')
        consolewindow = Toplevel(self.master)
        console = ConsoleWindow(consolewindow)
        consolewindow.mainloop()


    def updateConsole(self):

        string = 'Console Text !!'
        ConsoleWindow.insertText(string)

def main():

    mainWindow = Tk()
    mainGUI = Main(mainWindow)
    mainWindow.mainloop()

if __name__ == "__main__":
    main()

接下来,控制台窗口

from tkinter import *

class ConsoleWindow:

    def __init__(self, master):

        self.master = master
        self.master.title('Console Window')
        self.master.geometry('640x480')
        self.scrollBar = Scrollbar(self.master)
        self.textArea = Text(self.master)
        self.scrollBar.pack(side=RIGHT, fill=Y)
        self.textArea.pack(side=LEFT, fill=Y)
        self.scrollBar.config(command=self.textArea.yview)
        self.textArea.config(yscrollcommand=self.scrollBar.set)
        self.textArea.insert(INSERT, 'Running')
        self.textArea.configure(state='disabled')

    def insertText(self, textToUpdate):

        text = f'{textToUpdate}\n'
        self.textArea.configure(state='normal')
        self.textArea.insert(INSERT, text)
        self.textArea.configure(state='disabled')

我希望做的是在应用程序运行时使用insertText函数多次更新Text Widget。运行此代码时,我可以创建ConsoleWindow的实例,但是当我按下“更新控制台”按钮时,出现以下错误。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Aswin Kumar\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/Aswin Kumar/Console Window/Main.py", line 31, in updateConsole
    ConsoleWindow.insertText(string)
TypeError: insertText() missing 1 required positional argument: 'textToUpdate'

我尝试将ConsoleWindow的实例添加到insertText(),

ConsoleWindow.insertText(console, string)

出现以下错误,

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Aswin Kumar\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/Aswin Kumar/Console Window/Main.py", line 31, in updateConsole
    ConsoleWindow.insertText(console, string)
NameError: name 'console' is not defined

如何从主窗口更新控制台窗口。还是在这种情况下可以使用其他替代方法。

0 个答案:

没有答案