jonjonson
jonjonson

Reputation: 273

python tkinter how can I get output from function in a class and display it in textbox

Hey I want to display an output from function when I click button. My function is in a class and I want to display output in textbox in GUI. Can anyone help me here is the function code. For example I want to display from the draw function

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'

this would be my textbox in Gui tkinter witch I want my output to display

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

and the button which calls the draw function

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

#ignore

Upvotes: 0

Views: 1105

Answers (1)

Totem
Totem

Reputation: 7369

You could try something like the following:

Have the draw function call the insert method of output with the desired string

output.insert(END, string)

So instead of print 'Winner', you would do:

output.insert(END, "Winner")

syntax:

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

info on Text and more here

Upvotes: 1

Related Questions