Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python/Tkinter:如何从方法中访问ScrolledText小部件?_Python_User Interface_Oop_Methods_Tkinter - Fatal编程技术网

Python/Tkinter:如何从方法中访问ScrolledText小部件?

Python/Tkinter:如何从方法中访问ScrolledText小部件?,python,user-interface,oop,methods,tkinter,Python,User Interface,Oop,Methods,Tkinter,我想在Python/Tkinter中创建一个简单的、可重用的文本输出窗口类,它的功能与控制台输出类似,但具有更好的窗口 到目前为止,我设法从构造函数中直接将文本写入到ScrolledText小部件中,但这不是我想要的 我想创建一个print或println方法,允许我随时从主过程添加文本 如何从print方法中“调用”文本小部件 我的主要程序: from gui.simpleio import Textwindow a = Textwindow("This is my Tex

我想在Python/Tkinter中创建一个简单的、可重用的文本输出窗口类,它的功能与控制台输出类似,但具有更好的窗口

到目前为止,我设法从构造函数中直接将文本写入到
ScrolledText
小部件中,但这不是我想要的

我想创建一个
print
println
方法,允许我随时从主过程添加文本

如何从print方法中“调用”文本小部件

我的主要程序:

    from gui.simpleio import Textwindow 

    a = Textwindow("This is my Textwindow !")
    a.print("I'd like to see a hello from the print method")
    a.stxt.INSERT("but nothing happens")
    from tkinter import Tk, Frame, INSERT
    from tkinter import scrolledtext

    class Textwindow(Frame):

        def __init__(self , title):
            root = Tk()  
            stxt = scrolledtext.ScrolledText(root)
            stxt.pack()
            root.wm_title(title)
            stxt.insert(INSERT,"blah\nblabla\n")
            stxt.insert(INSERT,"yes\nno")
            root.mainloop()

        def print(self,text):
            #self.stxt.insert(INSERT,text)       
            pass
gui包中的My Textwindow类(simpleio.py):

    from gui.simpleio import Textwindow 

    a = Textwindow("This is my Textwindow !")
    a.print("I'd like to see a hello from the print method")
    a.stxt.INSERT("but nothing happens")
    from tkinter import Tk, Frame, INSERT
    from tkinter import scrolledtext

    class Textwindow(Frame):

        def __init__(self , title):
            root = Tk()  
            stxt = scrolledtext.ScrolledText(root)
            stxt.pack()
            root.wm_title(title)
            stxt.insert(INSERT,"blah\nblabla\n")
            stxt.insert(INSERT,"yes\nno")
            root.mainloop()

        def print(self,text):
            #self.stxt.insert(INSERT,text)       
            pass

为您的TextWindow提供一个写入方法,实例
a
将成为一个打开以供写入的文件。下面是Lib/idlelib/OutputWindow的OutputWindow.write的简化版本

# Act as output file
def write(self, s):
    self.text.insert('insert', s)
    self.text.see('insert')
    self.text.update()
    return len(s)

添加此项后,
print('Hello in TextWindow',file=a)
应能在3.x或2.7版本中使用print\u函数进行将来的导入。

请不要对所有内容都使用粗体,只对重要内容使用粗体。
TextWindow.print()
不起作用,因为
self.stxt
未在
TextWindow中定义。
此外,
TextWindow
\uuuu init\uuuu
方法调用
root.mainloop()
,因此以下
a.print
调用将在主窗口被销毁之前不会执行。请查看问题中的
errorwindow.py
模块。