我正在学习Tkinter
,我需要一个Button,然后当我点击它时,用某些代码做一些事情,然后打印结果。
我从这开始:
from Tkinter import *
import heapq
root = Tk()
root.title("TEST")
root.geometry("800x600")
#-------------------CODE-----------------------
def makeHuffTree(symbolTupleList):
trees = list(symbolTupleList)
heapq.heapify(trees)
while len(trees) > 1:
childR, childL = heapq.heappop(trees), heapq.heappop(trees)
parent = (childL[0] + childR[0], childL, childR)
heapq.heappush(trees, parent)
return trees[0]
def printHuffTree(huffTree, prefix = ''):
if len(huffTree) == 2:
print huffTree[1], prefix
else:
printHuffTree(huffTree[1], prefix + '0')
printHuffTree(huffTree[2], prefix + '1')
def OnButton():
exampleData = [(0.124167 , 'e'), (0.0969225 , 't'),(0.0820011 , 'a'),]
huffTree = makeHuffTree(exampleData)
printHuffTree(huffTree)
button = Button(root, text="Press me!", command=OnButton)
button.pack()
root.mainloop()
我现在如何制作一个GUI来输出结果?
答案 0 :(得分:2)
您希望在代码中添加以下内容:
def OnButton():
exampleData = [(0.124167 , 'e'), (0.0969225 , 't'),(0.0820011 , 'a'),]
huffTree = makeHuffTree(exampleData)
printHuffTree(huffTree)
button = Button(root, text="Press me!", command=OnButton)
button.pack()
如果通过'“打印”结果显示在GUI中。“你的意思是你希望结果显示在窗口而不是标准输出上,你需要添加类似文本小部件的内容并替换所有print
<textwidget>.insert
语句/