Python Tkinter按钮不显示问题求助
Tkinter Designer生成的UI按钮不显示问题解决
问题描述
使用Tkinter Designer生成UI后,程序运行无报错,但按钮始终不显示。已尝试调整按钮位置排除遮挡,重新生成UI到新文件可正常运行,但复制到当前文件后按钮仍无法显示。
问题代码
from tkinter import * import new_post from pathlib import Path from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage def home_page_screen(): # SETTING UP WINDOW top = Toplevel() top.resizable(height=False, width=False) # FUNCTIONS # NEW POST def new_post_win(): new_post.new_post_win() # =================================================================================================================== # UI # The UI section of the file was generated by the Tkinter Designer by Parth Jadhav OUTPUT_PATH = Path(__file__).parent ASSETS_PATH = OUTPUT_PATH / Path(r"assets\home_page") def relative_to_assets(path: str) -> Path: return ASSETS_PATH / Path(path) # WINDOW SETTINGS top.geometry("431x728") top.configure(bg="#FFFFFF") top.resizable(False, False) # ==========FRAME===================== canvas = Canvas( top, bg="#FFFFFF", height=728, width=431, bd=0, highlightthickness=0, relief="ridge" ) canvas.place(x=0, y=0) # ========TITLE======== title_image = PhotoImage( file=relative_to_assets("image_1.png")) title = canvas.create_image( 215.0, 48.0, image=title_image ) post_holder_image = PhotoImage( file=relative_to_assets("image_2.png")) post_holder = canvas.create_image( 215.0, 364.0, image=post_holder_image ) # ========POST=LABEL======== canvas.create_text( 27.0, 169.0, anchor="nw", text="", fill="#000000", font=("Karantina Regular", 32 * -1), width=385 ) # ========NEW=POST=LABEL======== new_post_image = PhotoImage( file=relative_to_assets("image_3.png")) new_post = canvas.create_image( 107.0, 134.0, image=new_post_image ) # ========POST=BUTTON======== post_btn_image = PhotoImage( file=relative_to_assets("button_1.png")) post_btn = Button( image=post_btn_image, borderwidth=0, highlightthickness=0, command=new_post_win, relief="flat" ) post_btn.place( x=25.0, y=500.0, width=194.0, height=64.0 ) # MAIN LOOP top.mainloop()
问题原因
Tkinter的PhotoImage对象如果没有被持久引用,会被Python的垃圾回收机制自动回收。你的代码中,post_btn_image是函数内的局部变量,当函数执行到末尾时,该变量会被销毁,导致按钮失去图片引用,从而无法显示。
解决方法
给所有PhotoImage对象添加持久引用,避免被垃圾回收:
1. 给按钮图片添加引用
修改按钮创建部分的代码,将post_btn_image绑定到按钮实例的属性上:
# ========POST=BUTTON======== post_btn_image = PhotoImage( file=relative_to_assets("button_1.png")) post_btn = Button( image=post_btn_image, borderwidth=0, highlightthickness=0, command=new_post_win, relief="flat" ) # 添加这行,保持图片引用 post_btn.image = post_btn_image post_btn.place( x=25.0, y=500.0, width=194.0, height=64.0 )
2. 给Canvas中的图片添加引用
同样,Canvas中使用的图片也需要添加引用,避免被回收:
# ========TITLE======== title_image = PhotoImage( file=relative_to_assets("image_1.png")) title = canvas.create_image( 215.0, 48.0, image=title_image ) # 绑定到canvas的属性上 canvas.title_image = title_image # ========POST HOLDER======== post_holder_image = PhotoImage( file=relative_to_assets("image_2.png")) post_holder = canvas.create_image( 215.0, 364.0, image=post_holder_image ) canvas.post_holder_image = post_holder_image # ========NEW POST LABEL======== new_post_image = PhotoImage( file=relative_to_assets("image_3.png")) new_post = canvas.create_image( 107.0, 134.0, image=new_post_image ) canvas.new_post_image = new_post_image
3. 验证修改
修改后重新运行程序,按钮应该能正常显示。如果仍有问题,检查图片路径是否正确,确保assets\home_page目录下存在button_1.png等图片文件。
内容的提问来源于stack exchange,提问作者Coding Master
相关产品推荐
相关产品推荐

