python tkinter如何从类中的函数获取输出并将其显示在文本框中

时间:2014-02-23 22:42:48

标签: python

嘿我想点击按钮时显示功能输出。我的函数在一个类中,我想在GUI中的文本框中显示输出。任何人都可以帮我这里是功能代码。例如,我想从绘图功能

中显示
from random import shuffle


class spilastokkur():

def __init__(self):
    self.spil=[]
    self.bord=[]
    self.hond=[]

def shuffle(self):
    spil= self.spil
    sort = ['H', 'S', 'T', 'L']
    num = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',]
    for i in range(0,4):
        for j in range(0, 13):
            spil.append(sort[i]+ num[j])
    shuffle(spil)
    #print spil
    return spil

def draw(self):
    hond = self.hond
    spil = self.spil
    if len(hond) >= 3:
        print 'Geturu fjarlaegt spil????'
    if len(spil) > 0:
        x = spil.pop(0)
        hond.append(x)
        print(hond)
    else:
        y = hond.pop(0)
        hond.append(y)
        print hond
        output.insert(hond.get())
        if len(hond)<=2:
            print 'winner'

def sort_cheat(self):
    hond = self.hond
    if len(hond) >= 4:
        del hond[-2]
        del hond[-2]
        print hond
        print 'You got rid of 2 cards. You can do better :)'
enter code here

def number(self):
    hond = self.hond
    if len(hond) >= 4:
        if (hond[-1][1]== hond[-4][1]):
            del hond[-4:]
            print hond
        else:
            print 'You are cheating'

这将是我在Gui tkinter中的文本框女巫我希望我的输出显示

output = Text(root, height=20, width=50) 
output.pack()

和调用绘图功能的按钮

#Buttons
Draw_button = Button(command=s.draw).pack()

#ignore

1 个答案:

答案 0 :(得分:1)

您可以尝试以下内容:

让draw函数使用所需的字符串

调用输出的insert方法
output.insert(END, string)

所以你可以这样做而不是print 'Winner',

output.insert(END, "Winner")

语法:

insert(index [,string]...)
This method inserts strings at the specified index location.

Text及更多here

的信息