Class Python3x-tkinter中的助手函数无法访问文本小部件
我正在从一本参考书中学习python tkiner。示例以普通样式编码,即不采用类格式。我想在课堂上学习编码,因为我认为它有助于管理长代码 我正试图使文本小部件(名为Class Python3x-tkinter中的助手函数无法访问文本小部件,class,user-interface,python-3.x,tkinter,global-variables,Class,User Interface,Python 3.x,Tkinter,Global Variables,我正在从一本参考书中学习python tkiner。示例以普通样式编码,即不采用类格式。我想在课堂上学习编码,因为我认为它有助于管理长代码 我正试图使文本小部件(名为textPad)可供名为textdeditor的类中的助手函数访问。helper函数的任务是选择我键入的所有文本。但是,当我运行脚本时,我得到一个全局错误,即未定义textPad。即使我将self.添加到textPad,即self.textPad,我也会得到一个属性错误,即类对象没有属性textPad。该代码是制作全功能文本编辑器的
textPad
)可供名为textdeditor
的类中的助手函数访问。helper函数的任务是选择我键入的所有文本。但是,当我运行脚本时,我得到一个全局错误,即未定义textPad
。即使我将self.
添加到textPad
,即self.textPad
,我也会得到一个属性错误,即类对象没有属性textPad。
该代码是制作全功能文本编辑器的练习的一部分。下面,我提供生成错误的核心代码。这个代码有什么问题
请你澄清我的疑问:在什么地方定义帮助函数最好:类内还是类外?在这两种情况下,如何使它们可访问
from tkinter import *
class TextEditor():
def __init__(self, root):
self.select_all() #helper function declare
myMenu = Menu(root, tearoff=0) #Menu bar
editMenu = Menu(root, tearoff)
editMenu.add_command(label="Select All", accelerator="Ctrl+A", command=select_all)
myMenu.add_cascade(label="Edit", menu=editMenu)
root.config(menu=myMenu)
textPad = Text(root, wrap="word", undo=True)
textPad.pack(expand="yes", fill="both")
def select_all(self):
textPad.tag_add('sel', '1.0', 'end')
if __name__ == '__main__':
root=Tk()
app = TextEditor(root)
root.mainloop()
这就是错误:
Traceback (most recent call last):
File "C:\Python33\gui\tkguibook\textpad.py", line 21, in <module>
app = TextEditor(root)
File "C:\Python33\gui\tkguibook\textpad.py", line 6, in __init__
self.select_all() #helper function declare
File "C:\Python33\gui\tkguibook\textpad.py", line 17, in select_all
textPad.tag_add('sel', '1.0', 'end')
NameError: global name 'textPad' is not defined
回溯(最近一次呼叫最后一次):
文件“C:\Python33\gui\tkguibook\textpad.py”,第21行,在
app=文本编辑器(根)
文件“C:\Python33\gui\tkguibook\textpad.py”,第6行,在\uuu init中__
self.select_all()#helper函数declare
文件“C:\Python33\gui\tkguibook\textpad.py”,第17行,在全选中
textPad.tag_add('sel','1.0','end')
NameError:未定义全局名称“textPad”
提前感谢您的帮助 首先,我建议您在不直接使用tkinter的情况下观看一些Python中面向对象范例的教程 代码的问题在于
textPad
不是类的属性,但它是\uuuu init\uuuu
方法或构造函数的简单局部变量。要使它成为一个属性,您应该使用self
来声明,然后引用刚刚声明的属性
例如,假设我有以下类:
class TextEditor:
def __init__(self):
# stuff
如果要添加一个在类中的所有点上都可见的属性,可以通过以下方式执行:
class TextEditor:
def __init__(self):
self.textPad = tkinter.Text() # using 'self' to declare a property
现在,如果要在另一个方法中引用该属性,则应始终使用self
:
class TextEditor:
def __init__(self):
self.textPad = tkinter.Text()
def set_text(self, new_text):
self.textPad.insert(tkinter.END, "hello") # using 'self' to refer to the property
要了解更多。首先,我建议您在不直接使用tkinter的情况下观看一些Python中面向对象范例的教程 代码的问题在于
textPad
不是类的属性,但它是\uuuu init\uuuu
方法或构造函数的简单局部变量。要使它成为一个属性,您应该使用self
来声明,然后引用刚刚声明的属性
例如,假设我有以下类:
class TextEditor:
def __init__(self):
# stuff
如果要添加一个在类中的所有点上都可见的属性,可以通过以下方式执行:
class TextEditor:
def __init__(self):
self.textPad = tkinter.Text() # using 'self' to declare a property
现在,如果要在另一个方法中引用该属性,则应始终使用self
:
class TextEditor:
def __init__(self):
self.textPad = tkinter.Text()
def set_text(self, new_text):
self.textPad.insert(tkinter.END, "hello") # using 'self' to refer to the property
要了解更多。首先,我建议您在不直接使用tkinter的情况下观看一些Python中面向对象范例的教程 代码的问题在于
textPad
不是类的属性,但它是\uuuu init\uuuu
方法或构造函数的简单局部变量。要使它成为一个属性,您应该使用self
来声明,然后引用刚刚声明的属性
例如,假设我有以下类:
class TextEditor:
def __init__(self):
# stuff
如果要添加一个在类中的所有点上都可见的属性,可以通过以下方式执行:
class TextEditor:
def __init__(self):
self.textPad = tkinter.Text() # using 'self' to declare a property
现在,如果要在另一个方法中引用该属性,则应始终使用self
:
class TextEditor:
def __init__(self):
self.textPad = tkinter.Text()
def set_text(self, new_text):
self.textPad.insert(tkinter.END, "hello") # using 'self' to refer to the property
要了解更多。首先,我建议您在不直接使用tkinter的情况下观看一些Python中面向对象范例的教程 代码的问题在于
textPad
不是类的属性,但它是\uuuu init\uuuu
方法或构造函数的简单局部变量。要使它成为一个属性,您应该使用self
来声明,然后引用刚刚声明的属性
例如,假设我有以下类:
class TextEditor:
def __init__(self):
# stuff
如果要添加一个在类中的所有点上都可见的属性,可以通过以下方式执行:
class TextEditor:
def __init__(self):
self.textPad = tkinter.Text() # using 'self' to declare a property
现在,如果要在另一个方法中引用该属性,则应始终使用self
:
class TextEditor:
def __init__(self):
self.textPad = tkinter.Text()
def set_text(self, new_text):
self.textPad.insert(tkinter.END, "hello") # using 'self' to refer to the property
了解更多信息。非常感谢您的回复。我同意你的观点,我必须先看OOP教程。不幸的是,我尝试按照您的建议对代码进行建模,但没有成功。再次感谢。@Mohammed我可以试着修复你的代码,但这需要我一些时间,我不需要太多时间。按照我的建议,试着先看看面向对象编程(OOP)在Python中是如何工作的,一步一步地学习,它更高效,看看其他例子,你很快就会管理OOP:)好的,我会做的。我同意你的观点,没有什么比自学更有效。这是问题的一部分,另一部分是你调用
self。在定义self.textPad=Text(…)
之前,请选择所有()。因此,当运行行self.textPad.tag_add('sel','1.0','end')
时,self.textPad
还不存在。非常感谢您的回复。我同意你的观点,我必须先看OOP教程。不幸的是,我尝试按照您的建议对代码进行建模,但没有成功。再次感谢。@Mohammed我可以试着修复你的代码,但这需要我一些时间,我不需要太多时间。按照我的建议,试着先看看面向对象编程(OOP)在Python中是如何工作的,一步一步地学习,它更高效,看看其他例子,你很快就会管理OOP:)好的,我会做的。我同意你的观点,没有什么比自学更有效。这是问题的一部分,另一部分是你调用self。在定义self.textPad=Text(…)
之前,请选择所有()。所以当行self.textPad.tag_添加('sel','