0

I'm just working on a GUI with tkinter in python. But I'm having some issues here. I'm creating a button that once it's clicked it will go back a page (Misc.lower(canvas2) and Misc.lift(canvas1)). Although i created a window for the button on the self.canvas2, the buttons is still there when the canvas is lowered.

Here is my code:

from tkinter import *

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

        self.canvas1 = Canvas(self.window, highlightthickness=0, bg="#1b1b1b")
        self.canvas2 = Canvas(self.window, highlightthickness=0, bg="red2")

        self.initButtons()

        self.window.mainloop()

    def initButtons(self, *args):
        buttons = {}
        self.buttonList = []
        buttons['backButtons'] = Button(self.window, bd=0, activebackground="#1b1b1b", bg="#1b1b1b", text='Back', fg="#ffffff")
        self.buttonList.append(buttons['backButtons'])

        self.initSecondWindow()

    def initFirstWindow(self, *args):
        Misc.lower(self.canvas2)
        self.canvas1.place(relwidth=1, relheight=1)

    def initSecondWindow(self, *args):
        backButton = self.buttonList[0]
        backButton.config(command=self.initFirstWindow)
        self.canvas2.place(relwidth=1, relheight=1)

        self.canvas2.create_window(640, 360, window=backButton)

Application()

I read a bit about canvas.delete(), but I'm not sure how it works.

Thanks in advance!

Itsjul1an
  • 301
  • 1
  • 2
  • 12
  • 1
    Exactly what "issues" are you having? – martineau Jun 13 '21 at 18:35
  • Well actually I asked for a solution to get rid of the button when i lower the canvas where i created the button in. (Edited the question) – Itsjul1an Jun 13 '21 at 18:40
  • Here's some documentation on the [`Canvas.delete()`](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/canvas-methods.html) method. To summarize, whenever you create a `Canvas` item, a unique id is returned, which you can later pass to `delete()` to remove it. Note also that it is *not* considered an error to attempt to delete something that doesn't exist. Also note that you can show and hide *existing* canvas items using `canvas.itemconfigure(id, state='hidden'/'normal')`. – martineau Jun 14 '21 at 12:56
  • Depending on what it is you're trying to accomplish, consider using `Frames` to display different "pages" — see accepted answer to [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter). – martineau Jun 14 '21 at 12:58

1 Answers1

1

Not sure why you would just want to hide it though, then how would the users redirect to the previous page? Is this what you wanted?

from tkinter import *

class Application:
    def __init__(self):
        # self.backButton = None
        self.window = Tk()
        self.window.geometry("1280x720")

        self.canvas1 = Canvas(self.window, highlightthickness=0, bg="#1b1b1b")
        self.canvas2 = Canvas(self.window, highlightthickness=0, bg="red2")

        self.initButtons()

        self.window.mainloop()

    def initButtons(self, *args):
        buttons = {}
        self.buttonList = []
        buttons['backButtons'] = Button(self.window, bd=0, activebackground="#1b1b1b", bg="#1b1b1b", text='Back', fg="#ffffff")
        self.buttonList.append(buttons['backButtons'])

        self.initSecondWindow()

    def initFirstWindow(self, *args):
        Misc.lower(self.canvas2)
        self.canvas2.delete(self.res)
        self.canvas1.place(relwidth=1, relheight=1)

    def initSecondWindow(self, *args):
        backButton = self.buttonList[0]
        backButton.config(command=self.initFirstWindow)
        self.canvas2.place(relwidth=1, relheight=1)

        self.res = self.canvas2.create_window(640, 360, window=backButton)

Application()
Omid Ki
  • 155
  • 3
  • 13