pythontkintercanvas

The text does not show


I have a tkinter program that should place text. It does not. Why? The canv calls addText(), but the text does not show. I also tried not using my custom module, but still no effect.

import tkinter as tk
import terkinter as tr
import os
import re

win = tk.Tk()
allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
win.title("Terminos")
canv = tr.Trk(win,15,30)
canv.addText(6,6,"hello world!")
canv.canvas.create_text(canv.screenwidth-5,canv.screenheight-5,text=f"hello world!")
win.geometry(f'{canv.screenwidth}x{canv.screenheight}')

win.mainloop()

Why isn't the text showing? The terkinter module is as follows:

import tkinter as tk

class Trk:
    """
    The class of creating
    a canvas, and
    easily manipulating it
    with the TUI functions.
    """
    global screenWidth, screenHeight
    def __init__(self,win,blocksx,blocksy,bg="#232627"):
        self.screenwidth = win.winfo_screenwidth()
        self.screenheight = win.winfo_screenheight()
        self.canvas = tk.Canvas(win,width=self.screenwidth,height=self.screenheight,background=bg)
        self.canvas.pack()
        self.background = bg
        self.parent = win
        self.winwidth = win.winfo_width()
        self.winheight = win.winfo_height()
        self.width = self.canvas.winfo_width()
        self.height = self.canvas.winfo_height()
        self.blockx = blocksx
        self.blocky = blocksy
        self.blocks = self.blockList()
    def coordinate(self,row,col):
         return row*self.blockx, (self.winheight-col)*self.blocky
    def blockList(self):
        li = []
        for i in range(self.blockx):
            tmp = []
            for j in range(self.blocky):
                 tmp.append(None)
            li.append(tmp)
        return li
    def addText(self,row,col,text,font="1:1",color="white"):
        row, col = self.coordinate(row,col)
        self.canvas.create_text(row,col,text=text,fill=color)
    def kill(self):
        self.canvas.destroy()


Solution

  • Why isn't the text showing? The terkinter module is as follows:

    It is because it is being drawn off-screen

    Your coordinates are calculated as 90, 5820. My monitor is only 2160 pixels tall. Unless your monitor is more than 5820 pixels tall you won't be able to see it.

    The culprit is the coordinates method combined with the values you've passed in. It's not clear what you're trying to do, but if you change coordinates to this, you'll see the text near the upper-left column:

    def coordinate(self,row,col):
         return row*self.blockx, col*self.blocky
    

    If you change it to this, it will appear at the bottom-left:

    def coordinate(self,row,col):
         return row*self.blockx, self.screenheight-(col*self.blocky)