-1
from Tkinter import *

master = Tk()

def callback():
   print "click!"

b = Button(master, text="OK", command=callback)
b.pack()

mainloop()

现在在 iPython 中运行它会打印“点击!” 到控制台。如果我希望脚本或函数的结果出现在 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()
4

1 回答 1

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()
于 2013-08-14T02:29:22.663 回答