-1
import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        super().__init__()

        dict_words = {1 : "Canvas1",
                2 : "Canvas2",
                3 : "Canvas3",
                4 : "Canvas4",
                5 : "Canvas5",
                6 : "Canvas6",
                7 : "Canvas7",
                8 : "Canvas8"
                }
        for k,j in dict_words.items():
            b = tk.Button(self, width=20, text=j, padx=5, pady=5, command=lambda: open_canvas())
            b.pack()
        def open_canvas():
            self.canvas = tk.Canvas(self, height=300, width=300)
            self.canvas.pack()
            label = tk.Label(self.canvas, text="My canvas 1")
            label.grid()


if __name__ == '__main__':
    App().mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • it is fairly straightforward. I fail to see where you can get stuck. Use multiple Top-Level windows or multiple Frames or simple `grid_forget` and `grid` – FrainBr33z3 Mar 06 '20 at 10:24
  • I would need that every button opens the same frame, and inside the frame open different canvas – Mirco Sendo Mazzolena Mar 06 '20 at 11:00
  • ***"inside the frame open different canvas"***: You want to stacking `Canvas` and `.lift()` to show only one at the top? Use `self.canvas.grid(row=0, column=0)` instead of `.pack()` [Edit] your question an explain in detail **why** you want to use `Canvas` instead of `Frame`. Read [Switch between frames: `create=>tkraise()=>hide`](https://stackoverflow.com/a/7557028/7414759) – stovfl Mar 06 '20 at 11:08
  • I need a single frame because there will be the button to go back to the main page, and the canvas because I can see several items inside with the scrollbar (sorry, but I do not speak English well) – Mirco Sendo Mazzolena Mar 06 '20 at 11:20
  • ***"the canvas ... with the scrollbar"***: OK, you need multiple scrollable `Frame`'s within a single window. Use `Button(...).grid(row=0, column=0)` and `ScrollFrame(...).grid(row=1, column=0)`. Read [Adding a scrollbar to a group of widgets in Tkinter](https://stackoverflow.com/a/3092341/7414759) and [Correctly extend a widget](https://stackoverflow.com/questions/54584673/how-to-keep-tkinter-button-on-same-row-as-label-and-entry-box/54591761) – stovfl Mar 06 '20 at 16:44
  • I have a window with many buttons, each button opens the same frame to me but inside the frame there is a different canvas according to the button I pressed – Mirco Sendo Mazzolena Mar 09 '20 at 10:45
  • inside the canvases I have to put the scroll bar, and then I have to put a button that adds entry inside the canvas – Mirco Sendo Mazzolena Mar 09 '20 at 10:47

1 Answers1

1

Here is an example of opening multiple Canvas widgets by the click of a Button:

import tkinter as Tk

def open_canvas(x):
    Tk.Misc.lift(canvas[x], aboveThis=None)

root = Tk.Tk()

f1 = Tk.Frame(root)
f1.pack(expand=1, fill=Tk.BOTH)

canvas = {1 : "Canvas1",
          2 : "Canvas2",
          3 : "Canvas3",
          4 : "Canvas4",
          5 : "Canvas5",
          6 : "Canvas6"}

colors = ['red', 'blue', 'green', 'magenta', 'cyan', 'yellow']

for k, v in canvas.items():
    Tk.Button(f1, width=20, text=v, padx=5, pady=5, command=lambda k=k: open_canvas(k)).grid(row=1, column=k)
    canvas[k] = Tk.Canvas(f1, bg=colors[k-1])
    canvas[k].grid(row=0,column=1, columnspan=6)

root.mainloop()
FrainBr33z3
  • 1,085
  • 1
  • 6
  • 12