我是 Python 的新手,我正在尝试使用 TkInter 创建一个 GUI。我遇到的问题是当我尝试显示条目文本时。以下是我为此任务制作的两个函数。我的代码中是否缺少某些内容?
这是我创建条目小部件的第一个函数:
def getArea():
x1 = StringVar()
x2 = StringVar()
y1 = StringVar()
y2 = StringVar()
#coor x1
labelX1 = Label(input, text="X: ").grid(row=1, column=1)
entryX1 = Entry(input, width=8, textvariable=x1).grid(row=1, column=2)
#coor x2
labelY1 = Label(input, text="Y: ").grid(row=2, column=1)
entryY1 = Entry(input, width=8, textvariable=y1).grid(row=2, column=2)
#coor y1
labelX2 = Label(input, text="X: ").grid(row=1, column=3)
entryX2 = Entry(input, width=8, textvariable=x2).grid(row=1, column=4)
#coor y2
labelY2 = Label(input, text="Y: ").grid(row=2, column=3)
entryY2 = Entry(input, width=8, textvariable=y2).grid(row=2, column=4)
x1.set("Defalut value x1")
x2.set("Defalut value x2")
y1.set("Defalut value y1")
y2.set("Defalut value y2")
coorx1 = x1.get()
coorx2 = x2.get()
coory1 = y1.get()
coory2 = y2.get()
button = Button(input, text='ok',command=lambda: showResults(coorx1,coorx2,coory1,coory2)).grid(row=1, column=5)
exitButton = Button(input, text='exit', command=input.destroy).grid(row=2,column=5)
input.mainloop()
这是我要显示字符串的第二个函数:
def showResults(x1,x2,y1,y2):
showInfo = Tk()
showInfo.title("Location Temperature")
showInfo.geometry("270x100+470+320")
print x1, x2, y1, y2
info1 = Label(showInfo, text=x1).pack()
info2 = Label(showInfo, text=x2).pack()
info3 = Label(showInfo, text=y1).pack()
info4 = Label(showInfo, text=y2).pack()
buttonClose = Button(showInfo, text='exit', command=showInfo.destroy).pack(side= RIGHT)
showInfo.mainloop()