Search code examples
pythonuser-interfacetkintercanvastext

How to print results iteratively on a canvas or text widget within a GUI made in Python?


I have made a simple GUI that has two buttons for a Square function and Exit button. I want to print the results on the canvas as shown in the expected results.

Here are my codes

from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
from PIL import Image, ImageTk
import os
import cv2
import numpy as np
import time


    class application(Tk):
        def __init__(self,parent):
            Tk.__init__(self,parent)
            self.parent = parent
            self.minsize(width=300,height=500)
            self.initialize()
            
        def initialize(self):
            self.grid_columnconfigure(0,weight=1)
            self.grid_columnconfigure(1,weight=1)
            self.grid_columnconfigure(2,weight=1)
            
            #Button widgets
            ###########################################################################################
            self.button1 = Button(self,text='Square',bg= 'blue',width=15,height=2, command =Square)
            self.button1.grid(column=0,row=1,sticky='W',pady=5)
            
            self.button3 = Button(self,text='Exit',bg= 'blue',width=10,height=2,command=self.destroy)
            self.button3.grid(column=1,row=5,sticky='W',pady=5)
            
            #Text widget for inserting the result
        ############################################################################################
        
        self.canvas = Canvas(self,width=230,height=180,state=NORMAL)
        self.canvas.grid(column=0,row=4,sticky='W',padx=100,pady=10)
        #self.canvas.configure(bg='green')
        
        #Label widget
        ############################################################################################

        
        self.label3 = Label(self,text="Square",bg = 'red',width=10,height=2,anchor="center")
        self.label3.grid(column=0,row=3,sticky='W',padx=120)
def Square(self):
    for i in range(1,10):
        y = i**2
        print(i, y)
        
if __name__ == "__main__":
    app = application(None)
    #font.nametofont('TkDefaultFont').configure(size=10)
    app['bg']='red'
    app.title("square")
    app.mainloop()

My expected results are given in the image below

enter image description here


Solution

  • Not entirely sure what you are asking. But, if you just want to create text on canvas use canvas.create_text(x, y, text).

    from tkinter import *
    from PIL import Image,ImageTk
    
    
    class application(Tk):
        def __init__(self,parent):
            Tk.__init__(self,parent)
            self.parent = parent
            self.minsize(width=300,height=500)
            self.initialize()
            
        def initialize(self):
            self.grid_columnconfigure(0,weight=1)
            self.grid_columnconfigure(1,weight=1)
            self.grid_columnconfigure(2,weight=1)
            
            #Button widgets
            ###########################################################################################
            self.button1 = Button(self,text='Square',bg= 'blue',width=15,height=2, command=self.Square)
            self.button1.grid(column=0,row=1,sticky='W',pady=5)
            
            self.button3 = Button(self,text='Exit',bg= 'blue',width=10,height=2,command=self.destroy)
            self.button3.grid(column=1,row=5,sticky='W',pady=5)
            
            #Text widget for inserting the result
        ############################################################################################
            
            self.canvas = Canvas(self,width=230,height=180,state=NORMAL)
            self.canvas.grid(column=0,row=4,sticky='W',padx=100,pady=10)
            #self.canvas.configure(bg='green')
            
            self.label3 = Label(self,text="Square",bg = 'red',width=10,height=2,anchor="center")
            self.label3.grid(column=0,row=3,sticky='W',padx=120)
            
        def Square(self):
            for i in range(1,10):
                y = i**2
                print(i, y)
                c_id = self.canvas.create_text(0, 20, text=f"{i}\t{y}", fill="blue", font="Times 14")
                bbox = self.canvas.bbox(c_id)
                self.canvas.coords(c_id, 100, bbox[3]*i)
    
    if __name__ == "__main__":
        app = application(None)
        #font.nametofont('TkDefaultFont').configure(size=10)
        app['bg']='red'
        app.title("square")
        app.mainloop()