Home > front end >  How to print output from a function of a class in tkinter gui window python?
How to print output from a function of a class in tkinter gui window python?

Time:05-12

import tkinter as tk
    
class Triangle:

    def __init__(self, height=2, base=3):
        self.height = height
        self.base = base
       
    def getHeight(self):
        return self.height
    
    def getBase(self):
        return self.base
    
    def setHeight(self, height):
        self.height = height
    
    def setBase(self, base):
        self.base = base
       
    def calcArea(self):
        self.area = (height*base) / 2
    
    def show(self):
        self.calcArea()
        print("Area of Triangle: {:.2f}".format(self.area))

    
class Rectangle:

    def __init__(self, length=4, width=5):
        self.length = length
        self.width = width
       
    def getLength(self):
        return self.length
    
    def getWidth(self):
        return self.width
    
    def setLength(self, length):
        self.length = length
    
    def setWidth(self, width):
        self.width = width
       
    def calcArea(self):
        self.area = length*width
    
    def show(self):
        self.calcArea()
        print("Area of Rectangle: {:.2f}".format(self.area))

    
class Parallelogram:

    def __init__(self, height=6, base=7):
        self.height = height
        self.base = base
       
    def getHeight(self):
        return self.height
    
    def getBase(self):
        return self.base
    
    def setHeight(self, height):
        self.height = height
    
    def setBase(self, base):
        self.base = base
       
    def calcArea(self):
        self.area = height*base
    
    def show(self):
        self.calcArea()
        print("Area of Parallelogram: {:.2f}".format(self.area))
    
def main():
    output = []
    for _ in range(8):
        randomChoice = random.randint(1,3)
    
        if randomChoice == 1:
            output.append(Triangle())
    
        elif randomChoice == 2:
            output.append(Rectangle())
    
        elif randomChoice == 3:
            output.append(Parallelogram())
    
    for result in output:
        result.display()
        if isinstance(shape, Triangle):
            shapeName = "Triangle"
        if isinstance(shape, Rectangle):
            shapeName = "Rectangle"
        if isinstance(shape, Parallelogram):
            shapeName = "Parallelogram"

        print("Area of {}: {:.2f}".format(shapeName, calcArea()))
    
    master = tk.Tk()
    master.title("Area")
    master.geometry("300x250")
    btn = tk.Button(master, text="Show Output", command=main)
    btn.pack()
    master.mainloop()

Above is my code for what I want to know for how can I just print the result in the window. I don't want to make a button where the result will print in Idle/Visual Studio Code/Pycharm instead I want to show the calculate in gui window. The above code is printing the result randomly 8 times, but what can i do make it print the result in GUI window. I tried making the button, but the button was only executing the result in Idle after I would click on the button.

CodePudding user response:

import tkinter as tk
import random

class Triangle:

    def __init__(self, height=2, base=3):
        self.height = height
        self.base = base

    def getHeight(self):
        return self.height

    def getBase(self):
        return self.base

    def setHeight(self, height):
        self.height = height

    def setBase(self, base):
        self.base = base

    def calcArea(self):
        self.area = (self.height * self.base) / 2

    def show(self):
        self.calcArea()
        return "Area of Triangle: {:.2f}".format(self.area)


class Rectangle:

    def __init__(self, length=4, width=5):
        self.length = length
        self.width = width

    def getLength(self):
        return self.length

    def getWidth(self):
        return self.width

    def setLength(self, length):
        self.length = length

    def setWidth(self, width):
        self.width = width

    def calcArea(self):
        self.area = self.length * self.width

    def show(self):
        self.calcArea()
        return "Area of Rectangle: {:.2f}".format(self.area)


class Parallelogram:

    def __init__(self, height=6, base=7):
        self.height = height
        self.base = base

    def getHeight(self):
        return self.height

    def getBase(self):
        return self.base

    def setHeight(self, height):
        self.height = height

    def setBase(self, base):
        self.base = base

    def calcArea(self):
        self.area = self.height * self.base

    def show(self):
        self.calcArea()
        return "Area of Parallelogram: {:.2f}".format(self.area)


def main():
    global var
    output = []
    shapeName = None
    results = ''
    for _ in range(8):
        randomChoice = random.randint(1, 3)

        if randomChoice == 1:
            output.append(Triangle())

        elif randomChoice == 2:
            output.append(Rectangle())

        elif randomChoice == 3:
            output.append(Parallelogram())

    for result in output:
        result.show()
        if isinstance(result, Triangle):
            shapeName = "Triangle"
        if isinstance(result, Rectangle):
            shapeName = "Rectangle"
        if isinstance(result, Parallelogram):
            shapeName = "Parallelogram"

        results  = "Area of {}: {}\n".format(shapeName, result.show())
    var.set(results)


master = tk.Tk()
master.title("Area")
master.geometry("300x250")
btn = tk.Button(master, text="Show Output", command=main)
btn.pack()
var = tk.StringVar(master, '')
Label = tk.Label(master, textvariable=var)
Label.pack()
master.mainloop()

Basically is the same as aria's answer but outputs results to the tkinter app

CodePudding user response:

import tkinter as tk
import random    

class Triangle:

    def __init__(self, height=2, base=3):
        self.height = height
        self.base = base
       
    def getHeight(self):
        return self.height
    
    def getBase(self):
        return self.base
    
    def setHeight(self, height):
        self.height = height
    
    def setBase(self, base):
        self.base = base
       
    def calcArea(self):
        self.area = (self.height*self.base) / 2
        return self.area
    
    def display(self):
        self.calcArea()
        #print("Area of Triangle: {:.2f}".format(self.area))

    
class Rectangle:

    def __init__(self, length=4, width=5):
        self.length = length
        self.width = width
       
    def getLength(self):
        return self.length
    
    def getWidth(self):
        return self.width
    
    def setLength(self, length):
        self.length = length
    
    def setWidth(self, width):
        self.width = width
       
    def calcArea(self):
        self.area = self.length*self.width
        return self.area
    
    def display(self):
        self.calcArea()
        #print("Area of Rectangle: {:.2f}".format(self.area))

    
class Parallelogram:

    def __init__(self, height=6, base=7):
        self.height = height
        self.base = base
       
    def getHeight(self):
        return self.height
    
    def getBase(self):
        return self.base
    
    def setHeight(self, height):
        self.height = height
    
    def setBase(self, base):
        self.base = base
       
    def calcArea(self):
        self.area = self.height*self.base
        return self.area
    
    def display(self):
        self.calcArea()
        #print("Area of Parallelogram: {:.2f}".format(self.area))
    
def main():
    output = []
    for _ in range(8):
        randomChoice = random.randint(1,3)
    
        if randomChoice == 1:
            output.append(Triangle())
    
        elif randomChoice == 2:
            output.append(Rectangle())
    
        elif randomChoice == 3:
            output.append(Parallelogram())
    
    for result in output:
        result.display()
        if isinstance(result, Triangle):
            shapeName = "Triangle"
        if isinstance(result, Rectangle):
            shapeName = "Rectangle"
        if isinstance(result, Parallelogram):
            shapeName = "Parallelogram"

        print("Area of {}: {:.2f}".format(shapeName, result.calcArea()))
main()

here you have the shapeName and result.calcArea() on the 107th line. Then you need to put the result to tkinter. Have fun :)

  • Related