我正在尝试使用Tkinter创建一个简单的GUI,当您按下按钮时,它会将Entry字段中的值添加到Text字段中的下一行。
from tkinter import *
#def onclick():
# pass
class MyApp:
def __init__(self):
window=Tk()
window.title('Multi Line Text Form')
frame = Frame(window)
frame.pack()
#self.fields ={}
l = Label(frame, text="Text to add below: ")
l.grid(row=0, column = 0)
#self.fields['name'] = Entry(frame)
name = Entry(frame)
#self.fields['name'].grid(row=0,column=1)
name.grid(row=0,column=1)
self.text = Text(frame, width=40, height=10)
self.text.grid(row=3,column=1)
self.text.insert(INSERT, "Hello.....")
self.text.insert(END, "Bye Bye.....")
msg = name.get()
updatebtn = Button(frame, text="Update Text Box", command=self.writeToLog(name))
updatebtn.grid(row=0, column=2)
window.mainloop()
def writeToLog(self, msg):
print(msg)
numlines = self.text.index('end - 1 line').split('.')[0]
#text['state'] = 'normal'
if numlines==4:
self.text.delete(1.0, 2.0)
if self.text.index('end-1c')!='1.0':
self.text.insert('end', '\n')
self.text.insert('end', msg)
#text['state'] = 'disabled'
if __name__=="__main__":
MyApp()
答案 0 :(得分:1)
您的按钮需要使用要调用的函数的名称进行配置。然后,此函数可以从条目小部件中获取值并将其放在文本小部件中:
def __init__(self):
...
updatebtn = Button(command=self.update_text)
...
self.name_entry = Entry(frame)
def update_text(self):
data = self.name_entry.get()
self.writeToLog(data)