Python Tkinter Button Not Showing Up

Question:

I used Tkinter Designer by Parth Jadhav to generate the UI of it, I have gotten no error but the button still doesnt show up

The Code:

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
    # https://github.com/ParthJadhav/Tkinter-Designer
    OUTPUT_PATH = Path(__file__).parent
    ASSETS_PATH = OUTPUT_PATH / Path(r"assetshome_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()

I have tried to move the button up and down just to make sure it wasnt behind any other thing but that didnt fix it.

I also regenerated the UI and placed it in a new file which had the same path to the images (as the code above) to see if the image was the problem, but when I ran it in the new file everything showed up. I then copied it over to this file and the problem was still there.

Asked By: Coding Master

||

Answers:

Pass top as the first argument to Button in line 88.

So change this:

post_btn = Button(
image=post_btn_image,

to this:

post_btn = Button(
top,
image=post_btn_image,

top is the "master" that is required for the label to be rendered.

Answered By: xenon134