我有一个窗口和一个基于框架的子类,该子类具有一个输入框和一个按钮,它们均来自Entrybox和Button的派生子类。按钮子类具有clickme(),需要将文本插入到输入框中。有办法吗?
root = Tk.tk()
class CalcFrame(tk.Frame):
...
...
myEntrybox = tk.Entry(self, width =20) #ENTRY BOX WIDGET INSIDE CalcFrame
myEntrybox.grid(column=0,row=0)
...
...
button_A = CalcButton(self,text="A")
button_B = CalcButton(self,text="B")
button_C = CalcButton(self,text="C")
...
#END OF CalcFrame
class CalcButton:
... #INIT
...
self["command"] = self.click_me
click_me(self):
# I want to update the text box with button's text(A,B or C)
#END OF CalcButton
这种click_me()方案可行吗?如果是,我该怎么做?按钮框和输入框都是Frame内的实例
下面的代码段应该可以解决您的问题。
import tkinter as tk
root = tk.Tk()
def set_text(text):
entry_box.delete(0,"end")
entry_box.insert(0, text)
entry_box = tk.Entry(width =50) #ENTRY BOX WIDGET INSIDE CalcFrame
entry_box.grid(column=0,row=0)
entry_box.pack()
btnSet = tk.Button(root, height=1, width=10, text="A", command=lambda:set_text("A"))
btnSet.pack()
btnSet = tk.Button(root, height=1, width=10, text="B", command=lambda:set_text("B"))
btnSet.pack()
btnSet = tk.Button(root, height=1, width=10, text="C", command=lambda:set_text("C"))
btnSet.pack()
root.mainloop()