By tvcsop
Dear Community! No matter how I try to write the code, one image always appears twice! What’s wrong?
import tkinter as tk
from tkinter import ttk
class Szulo(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)
        self.kep1 = tk.PhotoImage(file='../globalkep/z11.png')
        ttk.Button(self, image=self.kep1).pack(padx=5, pady=5)
class Gyerek(Szulo):
    def __init__(self, container):
        super().__init__(container)
        self.kep2 = tk.PhotoImage(file='../globalkep/p11.png')
        ttk.Button(self, image=self.kep2).pack(padx=5, pady=5)
class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('600x500+500+50')
        self.configure(bg='green')
        szulo = Szulo(self)
        szulo.pack(padx=20, pady=20)
        gyerek = Gyerek(self)
        gyerek.pack(padx=20, pady=20)
if __name__ == '__main__':
    app = App()
    app.mainloop()
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Heya,
The issue you’re facing is because the Gyerek class inherits from the Szulo class and, during initialization, it calls super().__init__(container). This means that when you create an instance of Gyerek, both the Szulo and Gyerek initialization code will be run, resulting in two images being created and displayed: the image from the parent class (kep1) and the one from the child class (kep2).
Since the child (Gyerek) class is designed to inherit from the parent (Szulo), it will always run the Szulo initialization, which packs the button with the kep1 image. Then the Gyerek class adds its own image (kep2), leading to both images being shown.
If you want only one image in the child class (Gyerek), you’ll need to modify the design a bit
You could move the button creation for kep1 into a separate method and call it only when needed:
import tkinter as tk
from tkinter import ttk
class Szulo(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)
        self.kep1 = tk.PhotoImage(file='../globalkep/z11.png')
    def create_button(self):
        ttk.Button(self, image=self.kep1).pack(padx=5, pady=5)
class Gyerek(Szulo):
    def __init__(self, container):
        super().__init__(container)
        self.kep2 = tk.PhotoImage(file='../globalkep/p11.png')
        ttk.Button(self, image=self.kep2).pack(padx=5, pady=5)
class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('600x500+500+50')
        self.configure(bg='green')
        szulo = Szulo(self)
        szulo.pack(padx=20, pady=20)
        szulo.create_button()
        gyerek = Gyerek(self)
        gyerek.pack(padx=20, pady=20)
if __name__ == '__main__':
    app = App()
    app.mainloop()
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.