英文:
I cannot define a function inside my class
问题
在你提供的代码中,你有一个问题是关于 command=clear
的部分,这里应该将 clear
指定为一个类方法,而不是一个独立的函数。此外,还有其他一些需要修复的问题,例如在 self.geometry = self.root.geometry('880x500')
行中应该使用 self.root.geometry('880x500')
,以及在 self.original_language
和 self.translated_language
的 value
属性中需要提供一个语言列表。修正后的代码如下:
import tkinter as tk
from tkinter import ttk
class Translate:
def __init__(self):
self.root = tk.Tk()
self.root.geometry('880x500')
def design(self):
text_box = tk.Text(self.root, height=15, width=40)
text_box.grid(row=0, column=0, padx=20, pady=20)
translate_button = tk.Button(self.root, text='Translate', font=('Arial', 20))
translate_button.grid(row=0, column=1, padx=10, pady=20)
translated_text = tk.Text(self.root, height=15, width=40)
translated_text.grid(row=0, column=2, padx=10, pady=20)
self.original_language = ttk.Combobox(self.root, width=50, values=language_list)
self.original_language.current(0) # Set the default selection
self.original_language.grid(row=1, column=0)
self.translated_language = ttk.Combobox(self.root, width=50, values=language_list)
self.translated_language.current(0) # Set the default selection
self.translated_language.grid(row=2, column=0)
self.clear_button = tk.Button(self.root, text='Clear', font=('Arial', 18), command=self.clear)
self.clear_button.grid(row=2, column=1)
def clear(self):
text_box.delete(1.0, tk.END)
translated_text.delete(1.0, tk.END)
def language_list(self):
# You should define the language_list here
return
def run(self):
self.root.mainloop()
if __name__ == '__main__':
t1 = Translate()
t1.design()
t1.run()
注意,你需要定义 language_list
变量来填充下拉框的值,并且你需要创建一个 Translate
对象并调用 run()
方法来启动你的应用程序。此外,我还将 clear
方法中的 self.original_language
和 self.translated_language
改为 text_box
和 translated_text
,因为这些是你想要清空的文本框。
英文:
I am trying to define a function inside of my class but still on line 31 'command=clear' does not see my function i created below. Please help, thank you!!
import tkinter as tk
from tkinter import *
import googletrans
import textblob
from tkinter import ttk,messagebox
class Translate:
def __init__(self):
self.root = tk.Tk()
self.geometry = self.root.geometry('880x500')
def design(self):
text_box = Text(self.root,height=15,width=40)
text_box.grid(row=0,column=0,padx=20,pady=20)
translate_button = Button(self.root,text='Translate',font=('Arial',20))
translate_button.grid(row=0,column=1,padx=10,pady=20)
translated_text = Text(self.root,height=15,width=40)
translated_text.grid(row=0,column=2,padx=10,pady=20)
self.original_language = ttk.Combobox(self.root,width=50,value=language_list)
self.original_language.current(21)
self.original_language.grid(row=1,column=0)
self.translated_language = ttk.Combobox(self.root,width=50,value=language_list)
self.translated_language.current(26)
self.translated_language.grid(row=2,column=0)
self.clear_button = Button(self.root,text='Clear',font=('Arial',18),command=clear)
self.clear_button.grid(row=2,column=1)
def clear(self):
self.original_language.delete(1.0,END)
self.translated_language.delete(1.0,END)
def language_list(self):
return
self.root.mainloop()
if __name__ == '__main__':
t1 = Translate()
t1.design()
I cannot make the functions work
答案1
得分: 1
以下是代码部分的翻译,不包括评论和问题的回答:
# Edit: 不要使用通配符导入 `from tkinter import *`,因为这会将所有 `tkinter` 的命名空间加载到你的模块的命名空间中。
# Edit: 忘记添加函数。
这是它看起来的样子吗?
- 第 23 和 27 行应为 `value=self.language_list`。
- 第 24 和 28 行,索引应为零。应为
self.translated_language.current(0)
- 第 31 行,应为 `self.clear`
- 第 41 行,注释掉 `#self.root.mainloop()`
- 在第 46 行添加 `tk.mainloop()`
在第 34 行,清除函数:
def clear(self):
self.original_language.delete(1.0, END)
self.translated_language.delete(1.0, END)
将这个:
def clear(self):
self.original_language.delete(1.0, END)
self.translated_language delete(1.0, END)
改为:
def clear(self):
self.original_language.delete('0', tk.END)
self.translated_language.delete('0', tk.END)
使用 `tk` 作为前缀重新编写代码片段:
import tkinter as tk
import googletrans
import textblob
from tkinter import ttk, messagebox
class Translate:
def __init__(self):
self.root = tk.Tk()
self.geometry = self.root.geometry('880x500')
def design(self):
text_box = tk.Text(self.root, height=15, width=40)
text_box.grid(row=0, column=0, padx=20, pady=20)
translate_button = tk.Button(self.root, text='Translate', font=('Arial', 20))
translate_button.grid(row=0, column=1, padx=10, pady=20)
translated_text = tk.Text(self.root, height=15, width=40)
translated_text.grid(row=0, column=2, padx=10, pady=20)
self.original_language = ttk.Combobox(self.root, width=50, value=self.language_list)
self.original_language.current(0)
self.original_language.grid(row=1, column=0)
self.translated_language = ttk.Combobox(self.root, width=50, value=self.language_list)
self.translated_language.current(0)
self.translated_language.grid(row=2, column=0)
self.clear_button = tk.Button(self.root, text='Clear', font=('Arial', 18), command=self.clear)
self.clear_button.grid(row=2, column=1)
def clear(self):
self.original_language.delete('0', tk.END)
self.translated_language.delete('0', tk.END)
def language_list(self):
return
# self.root.mainloop()
if __name__ == '__main__':
t1 = Translate()
t1.design()
tk.mainloop()
请注意,代码中提到的函数 self.language_list
并没有提供具体的实现,需要根据需要进行编写。此外,代码中可能有一些错误,需要根据实际情况进行调试和修复。
英文:
Edit: Don't used Wildcard from tkinter import *
Why because when you do that, you load all of tkinter's
namespace into your module's namespace.
Edit: Forgotten to add function.
Is this what it looks like?
- Line 23 and 27 should be
value=self.language_list
. - line 24 and 28, the index to zero. Should be
self.translated_language.current(0) - In line 31, shoulde be
self.clear
- In line 41, comment out
#self.root.mainloop()
- Add in line 46,
tk.mainloop()
In line 34, clear function:
def clear(self):
self.original_language.delete(1.0,END)
self.translated_language.delete(1.0,END)
Change this:
def clear(self):
self.original_language.delete(1.0,END)
self.translated_language.delete(1.0,END)
to:
def clear(self):
self.original_language.delete('0', tk.END)
self.translated_language.delete('0', tk.END)
Snippet re-work using prefix as tk
:
import tkinter as tk
import googletrans
import textblob
from tkinter import ttk, messagebox
class Translate:
def __init__(self):
self.root = tk.Tk()
self.geometry = self.root.geometry('880x500')
def design(self):
text_box = tk.Text(self.root,height=15,width=40)
text_box.grid(row=0,column=0,padx=20,pady=20)
translate_button = tk.Button(self.root,text='Translate',font=('Arial',20))
translate_button.grid(row=0,column=1,padx=10,pady=20)
translated_text = tk.Text(self.root,height=15,width=40)
translated_text.grid(row=0,column=2,padx=10,pady=20)
self.original_language = ttk.Combobox(self.root,width=50,value=self.language_list)
self.original_language.current(0)
self.original_language.grid(row=1,column=0)
self.translated_language = ttk.Combobox(self.root,width=50,value=self.language_list)
self.translated_language.current(0)
self.translated_language.grid(row=2,column=0)
self.clear_button = tk.Button(self.root,text='Clear',font=('Arial',18),command=self.clear)
self.clear_button.grid(row=2,column=1)
def clear(self):
self.original_language.delete('0', END)
self.translated_language.delete('0', END)
def language_list(self):
return
#self.root.mainloop()
if __name__ == '__main__':
t1 = Translate()
t1.design()
tk.mainloop()
Screenshot:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论