-1

I`m using Python with PIL

Short Version:
I want to display an Image in the background (which only works with Labels to my knowledge?) Every widget has this Label now as master. I also want to add widgets later (with said Label as master) with a binded function.

When I click on the Button, every widget except the newly created one disappears (simple Code below).

Question:
Are there commands to prevent this, or are there alternatives to Labels I can use, or is there a post already addressing this problem?

Long Version:
I'm programming Monopoly in Python. My design includes a Frame that is shared from two widgets. With a Button you can switch between those widgets. To make the design comfortable and more visual appealing, one of those widget (a Label) has the background of a wood table. When creating, everything is placed as it should be, but when I press a Button to add a widget into my 2 dimensional Frame Matrix (on the Label widget), everything on the Label but the new created widget disappears. When forgetting the Label calling it again, everything is as it should (with new widget)

Because of the many things I already added to this Label, reloading the Label won't be an option, the delay would be to significant. I have encountered this problem twice before but always managed to go around it somehow. While experiencing this problem now 3 times, I could never find any solution to this problem on my own or in the Internet...and trust me I searched (or searched with the wrong keywords, I don't know.)

If my goal isn't clear yet: I want to add widgets to a Label (or other widgets that support images), whenever I want (events) without every other child (widgets) of this Label to disappear.

The code below is a very simple code to show you an example for my problem. My actual code for my program has over 2K lines and my coding is not something that is easy to look at, so I won't include it.

from tkinter import*
import tkinter as tk


class Oberflaeche:
    def __init__(self, main):
        self.mainframe = Label(main,height= 1000,width = 1000, bg='azure4' )
        self.mainframe.pack()

        self.array1 = []

        # To resemble it (for me):
        for k in range(5):
            array2 = []
            self.array1.append(array2)
            for i in range(11):

               array2.append(Frame(self.mainframe, height=120, width=80, bg='red'))
               self.array1[k][i].grid(row=k, column=i, pady=(5, 5), padx=(1, 1))

        # Just for visual purpose:
        for n in range(3):
            aLabel = Label(master=self.array1[n][n],bg = 'blue')
            aLabel.pack()
            aLabel.bind('<Button-1>', self.show)

    # Something I want to add:
    def show(self,event):
        Label1 = Label(self.array1[2][3], bg = 'yellow')
        Label1.pack()


main = tk.Tk()
Oberflaeche = Oberflaeche(main)
main.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

The Solution: Canvas

Okay I somehow completely forgot about canvas, with Canvas it just works fine. I am sorry that I didnt think it through before posting it...This is my first time working with python(doesnt make it better) and I managed to only use Labels and Frames to build my Project (so I only thought about Labels and Frames to find a solution). I am sorry. I am still going to answer your questions. (I hope this okay when I answer them here?)

  1. Yeah I should start! I am just at the point where I think the time in renovating the code would be better spend finishing it.

2.Thanks! yeah I should have searched better!

3.The simple code displays the same Problem: When adding a widget to the Label(with an event), every other widget with the label as a master disappears. At least that happens on my screen. If you want I can add pictures

4.I may have communicated that bad: after dismantling my code I found that the Problem is with the Label(not image). The same Problem I would have with a Label with an Image, i have with only a label. That`s why I thought it would be simpler to show you an example with only a label(called mainframe)

Thanks for editing my Post to make it more visual appealing and fixing my grammar!