【发布时间】:2018-06-18 23:16:48
【问题描述】:
我的名字是罗德。我最近开始使用 OOP 进行编程,但对我来说还不是很清楚。我想让我的按钮从我的四个条目中获取信息,但我不知道如何对程序说同时从四个条目中获取信息。我知道我必须使用 get() 方法,但我不明白如何将它插入到类中,所以它会识别我的四个条目。谢谢!
from tkinter import *
from tkinter import ttk
class Application(Frame):
def __init__(self):
Frame.__init__(self)
self.grid()
def createButton(self,b_text,b_command,r,c):
self.newButton = Button(self, text=b_text,command=b_command)
self.newButton.grid(padx=20, pady=10, row=r,column=c)
def createEntry(self,px,r,c):
text = StringVar()
self.newEntry = Entry(self,width=8,textvariable=text)
self.newEntry.grid(padx=px, pady=10,row=r,column=c)
def printEntryData():
#code here
app = Application()
entry1 = app.createEntry(20,0,0)
entry2 = app.createEntry(20,0,1)
entry3 = app.createEntry(20,0,2)
entry4 = app.createEntry(20,0,3)
app.createButton("add",printEntryData,1,6)
app.mainloop()
【问题讨论】:
-
print(entry1.get(), entry2.get(), ...) -
@BryanOakley 仅在将
return text添加到“createEntry”方法时才有效。 -
@Novel:啊,是的。好点子。
标签: python python-3.x oop tkinter