1

I am messing around a bit with tkinter in python, and i want to add some buttons to my canvas screen. But when i run the script, the button is not showing up on the screen. It's a little complicated, but I am creating buttons from a for-loop, then appending them in a list.

This is my code:

from tkinter import * 

class Application:
    def __init__(self): 
        self.window = Tk()
        self.window.geometry("1280x720")
        self.window.resizable(False, False)        

        self.initHomeWindow()

        self.window.mainloop()

    def initHomeWindow(self):
        def initButtons():
            self.buttonList = []
            self.button_load = {}
            self.button = {}

            imgInfo = [
                    ['Abs', '105', '425.97', '310'], #1st one is button itself, 2nd one is width of the button, 3rd one is the x-position of the button, 4th one is y-position of the button. 
                    ['Arms', '123', '425.97', '370'],
                    ['Back', '117', '425.97', '430'],
                    ['Calves', '128', '848', '314.5'],
                    ['Delts', '121', '425.97', '490'],
                    ['Glutes', '128', '848', '364.5'],
                    ['Hams', '127', '848', '414.5'],
                    ['Pecs', '112', '425.97', '550'],
                    ['Quads', '128', '848', '464.5'],
                    ['Traps', '126', '425.97', '610']
            ]

            for x in range(len(imgInfo)):
                self.button[str(imgInfo[x][0])] = Button(self.window, width=int(imgInfo[x][1]), height=49)

                self.buttonList.append([self.button[str(imgInfo[x][0])], imgInfo[x][2], imgInfo[x][3]])

        initButtons()

        self.window.overrideredirect(False)
        self.window.geometry("1280x720")

        self.canvas2 = Canvas(self.window, highlightthickness=0, bg="#1b1b1b")
        self.canvas2.pack(fill=BOTH, expand=TRUE)

        for x in range(len(self.buttonList)):
            self.canvas2.create_window(float(self.buttonList[x][1]), float(self.buttonList[x][2]), window=self.buttonList[x][0])

Application()

enter image description here

When I run the script, the button is not showing up on the screen (see image)

I hope someone can help me with this problem, thanks in advance!

Itsjul1an
  • 301
  • 1
  • 2
  • 12
  • 3
    Please provide a runnable [mre] — don't just apologize for not having done so. – martineau May 29 '21 at 13:49
  • Right 1 sec then – Itsjul1an May 29 '21 at 13:52
  • @martineau i hope it works now! – Itsjul1an May 29 '21 at 13:59
  • The code example needs to be _minimal_. For example, if the problem is with adding buttons to a canvas, do we really need any images at all? Won't a button with only text work just as well? Also, is the animation code necessary to reproduce the problem? – Bryan Oakley May 29 '21 at 14:04
  • Sorry! I'm new to stackoverflow, I'm trying! – Itsjul1an May 29 '21 at 14:10
  • All that's need it something that attempts to put a `Button` into a `Canvas` that fails to do so. The process can be triggered by clicking on some other regular `Button`. – martineau May 29 '21 at 14:14
  • Okay now it should be minimal! Sorry for my dumbness :( – Itsjul1an May 29 '21 at 14:23
  • I think the problem is that the list isn't found. Because i'm defining the list inside a function. But when I define the list out of the function, the ```append``` doesn't work – Itsjul1an May 29 '21 at 15:50
  • Your new minimal program is perfect, and makes it much easier to understand the problem. However, the picture now isn't at all related to the code in the question. – Bryan Oakley May 29 '21 at 16:02
  • Changed that as well :) Thanks for the feedback, i'm learning a lot from this! – Itsjul1an May 29 '21 at 16:19

1 Answers1

1

Your buttons are underneath the canvas on the z-axis due to the order in which you create the buttons and canvas and the fact that the buttons are a child of the root window rather than the canvas.

It would be best to make the buttons children of the canvas, but you can also use the lift method to raise the buttons higher in the stacking order:

for x in range(len(self.buttonList)):
    self.buttonList[x][0].lift()
    ...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • The ```lift()``` doesn't work for me. How can i make them children of the canvas? – Itsjul1an May 29 '21 at 16:17
  • 1
    @Itsjul1an: the first positional parameter when creating a widget is the parent of the widget. So, create the canvas first, and then use `self.canvas2` rather than `self.window` when creating the buttons. – Bryan Oakley May 29 '21 at 16:20