我从谷歌翻译的,对语法很抱歉。我正在学习自己编程,需要一些帮助。我想通过单击按钮来清理条目小部件。问题是我不知道如何绑定“class MainWindow”类中的命令。
import tkinter as tk
from tkinter import ttk
class InserFrame(ttk.Frame):
def __init__(self):
super().__init__()
self.__create_widget()
def __create_widget(self):
ttk.Label(self, text="Label1").grid(row=0, column=0, sticky=tk.W)
keyword1 = ttk.Entry(self)
keyword1.focus()
keyword1.grid(row=0, column=1, sticky=tk.W)
ttk.Label(self, text="Label2").grid(row=1, column=0, sticky=tk.W)
keyword2 = ttk.Entry(self)
keyword2.grid(row=1, column=1, sticky=tk.W)
ttk.Label(self, text="Label3").grid(row=2, column=0, sticky=tk.W)
keyword3 = ttk.Entry(self)
keyword3.grid(row=2, column=1, sticky=tk.W)
ttk.Label(self, text="Label4").grid(row=3, column=0, sticky=tk.W)
keyword4 = ttk.Entry(self)
keyword4.grid(row=3, column=1, sticky=tk.W)
for widgets in self.winfo_children():
widgets.grid(pady=7)
class ButtonFrame(ttk.Frame):
def __init__(self):
super().__init__()
self.__create_widget()
def __create_widget(self):
ttk.Button(self, text="Button1").grid(row=0, column=0)
ttk.Button(self, text="Button2").grid(row=1, column=0)
ttk.Button(self, text="Button3").grid(row=2, column=0)
ttk.Button(self, text="Button4").grid(row=3, column=0)
for widget in self.winfo_children():
widget.grid(pady=5)
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("400x400")
self.title("Prova")
self.columnconfigure(0, weight=4)
self.columnconfigure(1, weight=1)
self.__create_widgets()
this---------> ttk.Button(self, text="Clear").grid(row=4, column=0, sticky="E")
def __create_widgets(self):
insert_frame = InserFrame()
insert_frame.grid(row=0, column=0, sticky=tk.W)
insert_button = ButtonFrame()
insert_button.grid(row=0, column=1, sticky=tk.E)
for widget in self.winfo_children():
widget.grid(padx=5, pady=5)
if __name__=="__main__":
main_window = MainWindow()
main_window.mainloop()`
您可以在构造 Button 对象时将回调函数分配给
command
参数。
def button_callback():
# Your clearing code here.
...
ttk.Button(self, text="Clear", command=button_callback)
我不知道当您可以直接在
__init__
函数中创建框架元素时,您是否有特定原因通过函数创建它们。这样它们就可以直接分配给实例变量。
但是,您可以为
MainWindow
类创建一个函数来删除条目的内容,并将此函数分配给 command
按钮的 "Clear"
属性:
import tkinter as tk
from tkinter import ttk, END
class InserFrame(ttk.Frame):
def __init__(self):
super().__init__()
# assign entries to instance variables to be accessed through frame instance
self.k1, self.k2, self.k3, self.k4 = self.__create_widget()
def __create_widget(self):
ttk.Label(self, text="Label1").grid(row=0, column=0, sticky=tk.W)
keyword1 = ttk.Entry(self)
keyword1.focus()
keyword1.grid(row=0, column=1, sticky=tk.W)
ttk.Label(self, text="Label2").grid(row=1, column=0, sticky=tk.W)
keyword2 = ttk.Entry(self)
keyword2.grid(row=1, column=1, sticky=tk.W)
ttk.Label(self, text="Label3").grid(row=2, column=0, sticky=tk.W)
keyword3 = ttk.Entry(self)
keyword3.grid(row=2, column=1, sticky=tk.W)
ttk.Label(self, text="Label4").grid(row=3, column=0, sticky=tk.W)
keyword4 = ttk.Entry(self)
keyword4.grid(row=3, column=1, sticky=tk.W)
for widgets in self.winfo_children():
widgets.grid(pady=7)
# return entries to access them
return keyword1, keyword2, keyword3, keyword4
class ButtonFrame(ttk.Frame):
def __init__(self):
super().__init__()
self.__create_widget()
def __create_widget(self):
ttk.Button(self, text="Button1").grid(row=0, column=0)
ttk.Button(self, text="Button2").grid(row=1, column=0)
ttk.Button(self, text="Button3").grid(row=2, column=0)
ttk.Button(self, text="Button4").grid(row=3, column=0)
for widget in self.winfo_children():
widget.grid(pady=5)
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("400x400")
self.title("Prova")
self.columnconfigure(0, weight=4)
self.columnconfigure(1, weight=1)
# assign created frame to instance variable
self.insert_frame = self.__create_widgets()
# give command argument to button with function to clear contents
ttk.Button(self, text="Clear", command=self.clear_inputs).grid(row=4, column=0, sticky="E")
def __create_widgets(self):
insert_frame = InserFrame()
insert_frame.grid(row=0, column=0, sticky=tk.W)
insert_button = ButtonFrame()
insert_button.grid(row=0, column=1, sticky=tk.E)
for widget in self.winfo_children():
widget.grid(padx=5, pady=5)
# retrun created frame to access it
return insert_frame
# function to clear the entries
def clear_inputs(self):
self.insert_frame.k1.delete(0, END)
self.insert_frame.k2.delete(0, END)
self.insert_frame.k3.delete(0, END)
self.insert_frame.k4.delete(0, END)
if __name__ == "__main__":
main_window = MainWindow()
main_window.mainloop()