请帮我个忙。我犯了一个错误
Traceback (most recent call last):
File "C:\Users\mniza\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "C:\Users\mniza\OneDrive\LOL\Nizpad_spell_check_04.py", line 1576, in aaa
return CustomWidget.text_get()
TypeError: text_get() missing 1 required positional argument: 'self'
这是如此烦人,因为我在谷歌上搜索,试图找到答案几乎有两天。我对编码和python都是新手。产生此错误的代码是:
def colour_certain_text(event):
class CustomWidget(tk.Frame):
def __init__(self, parent, label, default=""):
tk.Frame.__init__(self, parent)
super().__init__(parent)
self.text_entry_label = tk.Label(self, text= "Enter the text here")
self.text_font_style_combobox_label = tk.Label(self, text= "Font Style")
self.text_font_colour_entry_label = tk.Label(self, text= "Font Colour")
self.text_entry= ttk.Entry(self)
self.text_font_style_combobox = ttk.Combobox(self, values= ["Regular", "Bold", "Italic", "Bold Italic"])
self.text_font_colour_entry= tk.Entry(self, state= "readonly", fg= "#FFFFFF")
self.text_font_colour_select_button= ttk.Button(self, text= "Select Colour")
self.customwidget_destroy_button= tk.Button(self, text="⨉ Delete", cursor = "hand2", relief= "ridge")
self.customwidget_destroy_button.bind("<Enter>", lambda event: self.customwidget_destroy_button.config(bg= "#FF0000"))
self.customwidget_destroy_button.bind("<Leave>", lambda event: self.customwidget_destroy_button.config(bg= "SystemButtonFace"))
self.text_font_style_combobox.insert(0, font_mode.get())
self.text_font_style_combobox.config(state= "readonly")
self.text_entry_label.grid(row= 0, column= 0)
self.text_font_style_combobox_label.grid(row= 0, column= 1)
self.text_font_colour_entry_label.grid(row= 0, column= 2)
self.text_entry.grid(row= 1, column= 0)
self.text_font_style_combobox.grid(row= 1, column= 1 )
self.text_font_colour_entry.grid(row= 1, column= 2)
self.text_font_colour_select_button.grid(row= 0, column= 3, rowspan= 2)
self.customwidget_destroy_button.grid(row= 0, column= 4, rowspan= 2)
def text_get(self):
return self.text_entry.get()
def font_colour_get(self):
return self.text_font_colour_entry.get()
def font_style_get(self):
return self.text_font_style_combobox.get()
colour_certain_text_win= tk.Toplevel(notepad)
a= CustomWidget(colour_certain_text_win, label= "Hello")
a.pack()
def aaa():
a_= 0
return CustomWidget.text_get()
a_btn= tk.Button(colour_certain_text_win, command= aaa)
a_btn.pack()
colour_certain_text_win.mainloop()
我讨厌上课,因为我不太擅长。但现在我无法避免上课。请帮我弄清楚这里到底出了什么问题<否则我就要辞职了<哦,顺便说一下,我的代码真的很脏。如果你听不懂,请不要对我大喊大叫
您必须在类的实例上调用
text_get()
,但您试图在类本身上调用它。这就是为什么它说您缺少self
参数的原因。实例方法作为第一个参数自动传递给实例(例如:self
),但仅当您在实例上调用该方法时在您的特定示例中,应该使用
a.text_get()
,因为a
包含对CustomWidget
的特定实例的引用想象一下,如果您创建了两个名为
a
和b
的CustomWidget
实例。如果调用CustomWidget.text_get()
,python如何知道是从a
获取文本还是从b
获取文本?它知道的方式是对类的特定实例调用方法注意:您也可以通过
CustomWidget.text_get(a)
显式地告诉text_get
使用哪个小部件,但这不是最好的方法相关问题 更多 >
编程相关推荐