[英]How can I insert text in a text widget from another class in tkinter?
I'm trying to access a tkinter text widget from another class through instancing, but it's not being modified when called from outside class but works fine when the method is called inside the class. 我正在尝试通过实例化从另一个类访问tkinter文本小部件,但是当从外部类调用时不会对其进行修改,但是在内部类中调用该方法时效果很好。
I've also tried @staticmethod, though that doesn't seem to work either. 我也尝试过@staticmethod,尽管这似乎也不起作用。 I'd much prefer using an instanced object of the class though. 我还是更喜欢使用该类的实例对象。
So here's the main: 所以这是主要的:
if __name__ == "__main__":
mainapp = tk.Tk()
mainapp.title("Automatic Proofreader")
mainapp.configure(background = "gray")
mainapp.resizable(width = False, height = False)
Core(mainapp).grid(column = 0, row = 0, sticky = 'news')
TextDisplay(mainapp).grid(column = 5, row = 0, sticky = 'news')
mainapp.mainloop()
Here's the class and the method I need to access: 这是我需要访问的类和方法:
class TextDisplay(tk.Frame):
def setText(self, text):
self.displayout.config(state = "normal")
self.displayout.delete(1.0, tk.END)
#This inserts nothing when called from outside class
self.displayout.insert(tk.INSERT, text)
#But it inserts the correct text when called from this same class
self.displayout.config(state = "disabled")
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
verticalscroll = tk.Scrollbar(self)
self.displayout = tk.Text(self, font = ('comic sans', 20, 'bold'), height = 20, width = 40, bg = "gray", wrap = tk.WORD, yscrollcommand = verticalscroll.set, state= "disabled")
self.displayout.grid(columnspan = 4)
verticalscroll.grid(column = 5, sticky = 'ns')
verticalscroll.config(command = self.displayout.yview)
I'm first instancing this in another class like so self.displayclass = TextDisplay(mainapp)
我首先将此实例self.displayclass = TextDisplay(mainapp)
另一个类,例如self.displayclass = TextDisplay(mainapp)
and calling the method like so self.displayclass.setText(self.text)
并像这样调用方法self.displayclass.setText(self.text)
I noticed in debugging, that the value of text
variable inside the method is passed perfectly when called from outside the class (eg self.text
gets passed correctly as text
). 我在调试中注意到,从类外部调用时,方法内部的text
变量的值可以完美传递(例如self.text
被正确地传递为text
)。 But the insertion does not work. 但是插入不起作用。
EDIT : By "not working" I meant that, it does not insert anything at all. 编辑:“不工作”我的意思是,它根本不插入任何内容。 Sorry for not being clear. 抱歉,不清楚。
In short, to call a function on an object you must have a reference to an instance of the object. 简而言之,要在对象上调用函数,必须具有对该对象实例的引用。 That's a fundamental aspect of object-oriented programming. 这是面向对象编程的基本方面。
You don't necessarily have to use a global variable. 您不必一定要使用全局变量。 You can, or you can store it as an attribute on some other object. 您可以或者可以将其作为属性存储在其他对象上。
Here is a minimal example that works: 这是一个可行的最小示例:
if __name__ == "__main__":
mainapp = tk.Tk()
displayclass = TextDisplay(mainapp)
displayclass.grid(column = 5, row = 0, sticky = 'news')
displayclass.setText("hello, world")
mainapp.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.