Shaji Sivaraman
Shaji Sivaraman

Reputation: 11

Displaying image in a button using tkinter in Python is not working as intended

I am trying to implement a window using tkinter which should allow to navigate to another pages on clicking buttons. I want to add images to the button as relevant to the functionality of the navigating page. However, the image is not getting displayed but can see a borer of the image. The button functionality also not working.

Note: Trying this out for learning and copied code from below source

https://pythonprogramming.net/change-show-new-frame-tkinter/

import tkinter as tk
from tkinter import ttk
from tkinter import *


class main_window(tk.Tk):

    def __init__(self, *args, **kwargs):
        
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("HMS")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (landing_page, patient_page):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(landing_page)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

        
class landing_page(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Hospital Management")
        label.pack(pady=10,padx=10)
        patient_icon = PhotoImage(file= './patient_navigate.png')

        button = tk.Button(self, 
                            text="Patient Page",
                            image = patient_icon,
                            command=lambda: controller.show_frame(patient_page))
        button.pack()

class patient_page(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="`Patient Page")
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(landing_page))
        button1.pack()

app = main_window()
app.mainloop()



 

Upvotes: 1

Views: 22

Answers (0)

Related Questions