Home > Blockchain >  My tkinter window isn't showing all the elements that are present in it
My tkinter window isn't showing all the elements that are present in it

Time:01-09

I have created a window class and when it is imported into the main file it doesn't render all the objects that are associated with the main window class.

This is the code that I am using.

The window class:

from tkinter import *

base_path = "img/"

def btn_clicked():
    print("Button clicked ")

def center_coordinates(screen_width:int,screen_height:int,width:int,height:int):
    window_x = int((screen_height/2) - (75 (height/2)))
    window_y = int((screen_width/2) - (width/2))    
    return f"{width}x{height} {window_y} {window_x}"

class scan_code_screen(Tk):

    def __init__(self):
        Tk.__init__(self)
        self.wm_overrideredirect(False)
  self.geometry(center_coordinates(self.winfo_screenwidth(),self.winfo_screenheight(),550,350))

        self.title(" ")
        self.iconbitmap("img\icon.ico")
        self.resizable(False,False)     
              
    def window(self):
  
        background_img = PhotoImage(file=base_path "background.png")
        bg = Label(self, image=background_img).place(x=-2,y=-2)
        
        button_image = PhotoImage(file=base_path "button.png")
        button =Button(self,image=button_image,borderwidth=0,highlightthickness=0,command=btn_clicked,relief=FLAT)
        button.place(x=210,y=253)

The main file:

from windows.scan_code import *

if __name__ == "__main__":
    testObj2 = scan_code_screen()
    testObj2.window()
    testObj2.mainloop()

CodePudding user response:

You need to keep a reference to the PhotoImage you created, it is getting garbage collected. Try something like this:

from tkinter import *

base_path = "img/"


def btn_clicked():
    print("Button clicked ")


def center_coordinates(screen_width:int,screen_height:int,width:int,height:int):
    window_x = int((screen_height/2) - (75 (height/2)))
    window_y = int((screen_width/2) - (width/2))
    
    return f"{width}x{height} {window_y} {window_x}"


class scan_code_screen(Tk):

    
    def __init__(self):
        Tk.__init__(self)
        
        self.wm_overrideredirect(False)

        self.geometry(center_coordinates(self.winfo_screenwidth(),self.winfo_screenheight(),550,350))

        self.title(" ")
        self.iconbitmap("img\icon.ico")

        self.resizable(False,False)
              

    def window(self):
  
        self.background_img = PhotoImage(file=base_path "background.png")
        bg = Label(self, image=self.background_img).place(x=-2,y=-2)
        
        self.button_image = PhotoImage(file=base_path "button.png")
        button =Button(self,image=self.button_image,borderwidth=0,highlightthickness=0,command=btn_clicked,relief=FLAT)
        button.place(x=210,y=253)

CodePudding user response:

The center_coordinates and btn_clicked should be inside class module. Use self for all widgets.

Btw, I didn't not add photo. I leave it up to you.

I modified code:

from tkinter import *


base_path = "img/"

class scan_code_screen(Tk):
    def __init__(self):
        Tk.__init__(self)
         
        self.wm_overrideredirect(False)
        self.geometry(self.center_coordinates(self.winfo_screenwidth(),self.winfo_screenheight(),550,350))         
        self.title("icon.ico")
        self.iconbitmap("img\icon.ico")
        self.resizable(False,False)
            
    def center_coordinates(self, screen_width:int,screen_height:int,width:int,height:int):
        window_x = int((screen_height/2) - (75 (height/2)))
        window_y = int((screen_width/2) - (width/2))    
        return f"{width}x{height} {window_y} {window_x}"

    def btn_clicked(self):
        print("Button clicked ")
        self.bg.configure(text="Button clicked ")
                  
    def window(self):  
        background_img = PhotoImage(file=base_path "background.png")
        
        self.bg = Label(self, text='Text')
        self.bg.place(x=-2,y=-2)
        
        button_image = PhotoImage(file=base_path "button.png")
        
        self.button =Button(self, text='button',
                            borderwidth=0,
                            highlightthickness=0,
                            command=self.btn_clicked,
                            relief=FLAT)
        
        self.button.place(x=210,y=253)

if __name__ == "__main__":     
    testObj2 = scan_code_screen()
    testObj2.window() 
    testObj2.mainloop() 

   

Output before:

enter image description here

Output after:

enter image description here

  • Related