from Tkinter import *
master = Tk()
def callback():
print "click!"
b = Button(master, text="OK", command=callback)
b.pack()
mainloop()
现在在iPython中运行它会打印“click!”到控制台。如果我想在GUI框中显示脚本或函数的结果,在按钮下我该如何实现?盒子的大小是否需要先进分配?
编辑:我想要调用的函数实际上比上面的回调更复杂。当我运行以下代码时,它打印
而不是打印clusOne.head()<function centroid at 0x2cf3410>
到输出框。我希望能够打印由此函数产生的数据行而不是指针地址。
master = Tk()
# The output box prints an address (pointer) as a result of running this function.
#I would like to see the output in the box.
df=pd.read_csv('8162013.csv')
df=df.set_index('date1')
# Initialize the centroid.
cen1=df.mean()
v=ny.random.randn()+10
cen2=df.mean()-v
train=df[0:1615]
def centroid(train,cen1,cen2):
for i in range(0,3):
# Sum of squares. Results in a series containing 'date' and 'num'
sorted1=((train-cen1)**2).sum(1)
sorted2=((train-cen2)**2).sum(1)
# This makes a list of the cluster1 and cluster2
a=[train.ix[i] for i in train.index if sorted1[i]<sorted2[i]]
b=[train.ix[i] for i in train.index if sorted1[i]>=sorted2[i]]
# Back to a dataframe...
clusOne=pd.DataFrame(a,columns=['ES','US','GC'])
clusTwo=pd.DataFrame(b,columns=['ES','US','GC'])
# Update the centroid.
cen1=clusOne.mean()
cen2=clusTwo.mean()
print clusOne.head()
print "I'm computing your centroid."
def callback():
listbox.insert(END, centroid)
b = Button(master, text="Cluster", command=callback)
listbox = Listbox(master)
b.pack()
listbox.pack()
mainloop()
答案 0 :(得分:0)
您可以创建一个tkinter列表框,并在每次有人按下按钮时向其添加文本:
from Tkinter import *
master = Tk()
def callback():
listbox.insert(END, "Click!")
b = Button(master, text="OK", command=callback)
listbox = Listbox(master)
b.pack()
listbox.pack()
mainloop()