Tkinter程序文本无法显示:自定义模块及直接调用均无效求助
Tkinter文本无法显示问题排查
问题描述
开发的Tkinter程序无法显示文本,调用自定义terkinter模块的addText方法无效,直接调用canvas的create_text方法也看不到文本。
主程序代码
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()
自定义terkinter模块代码
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()
问题原因分析
- 窗口尺寸初始化时机错误:创建
Trk实例后才设置win.geometry,导致__init__中调用win.winfo_width()和win.winfo_height()获取的是窗口默认最小尺寸(通常为1x1),后续coordinate方法计算出的坐标完全不符合预期。 - 文本坐标超出可见区域:直接调用
create_text时使用canv.screenwidth-5和canv.screenheight-5作为坐标,窗口标题栏会占用部分高度,导致文本被窗口底部边框遮挡,无法显示。 - 坐标计算逻辑错误:
coordinate方法中(self.winheight-col)*self.blocky的计算方式,在winheight极小的情况下会得到负数或超出canvas范围的坐标,文本被绘制到不可见区域。
修复方案
1. 调整窗口尺寸设置时机
先设置窗口尺寸,再创建Trk实例,确保初始化时能获取正确的窗口大小。
2. 修正坐标计算逻辑
调整coordinate方法的计算方式,基于canvas的实际尺寸而非窗口初始尺寸,同时避免负数坐标。
3. 调整直接绘制文本的坐标
预留足够的边距,避免文本被窗口边框遮挡。
修复后的代码
主程序代码
import tkinter as tk import terkinter as tr import os import re win = tk.Tk() allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" win.title("Terminos") # 先设置窗口尺寸 screen_width = win.winfo_screenwidth() screen_height = win.winfo_screenheight() win.geometry(f'{screen_width}x{screen_height}') # 强制更新窗口,确保获取正确的尺寸 win.update_idletasks() # 再创建Trk实例 canv = tr.Trk(win,15,30) canv.addText(6,6,"hello world!") # 调整坐标,预留边距 canv.canvas.create_text(screen_width-20, screen_height-40, text=f"hello world!", fill="white") win.mainloop()
自定义terkinter模块代码
import tkinter as tk class Trk: """ 创建画布并通过TUI函数轻松操作的类 """ 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(fill=tk.BOTH, expand=True) # 让画布填充整个窗口 self.background = bg self.parent = win # 强制更新后获取正确的窗口和画布尺寸 win.update_idletasks() 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): # 修正坐标计算,确保在画布可见范围内,避免负数 x = row * self.blockx # 从画布顶部开始计算y坐标,而非窗口高度减col y = col * self.blocky # 确保坐标不超出画布范围 x = max(0, min(x, self.width)) y = max(0, min(y, self.height)) return x, y 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=("Arial",12),color="white"): row, col = self.coordinate(row,col) self.canvas.create_text(row,col,text=text,fill=color, font=font) def kill(self): self.canvas.destroy()
内容的提问来源于stack exchange,提问作者Felix508
相关产品推荐
相关产品推荐

