1

I am creating a book with Tkinter, I have defined each pages of my book to a canvas and I want to be able to shift between pages by closing the current canvas (current page) and open the next canvas (next page).

I have two problems:

  1. The problem is that when I use the command Canvas.delete(can1) the canvas does not close (I don't know why...), so I tried Canvas.destroy(can1), but then when I want to go to the previous page I can't because I destroyed it (I think that this is the problem). Is there an other function that does the same job of deleting the canvas from my main window, but then if i want to reopen it I can?

  2. If you look at the code (at the end), I need to use a Button to open the first page because I can't put the 'page1()' in the mainloop(). Is there a command to call a function once at the start of a program? So that when I run it the first page opens automatically... (I hope you know/understand what I'm searching).

I apologies for my awful english and for the french in the code.. ;)

The code is in python 3.6!

from tkinter import *
ModeleProiePredateur=Tk()
ModeleProiePredateur.title('Le Modele Proie-Predateur')

Largeur=1047
Hauteur=740

x=1

can1 = Canvas(ModeleProiePredateur,width=Largeur,height=Hauteur)
can2 = Canvas(ModeleProiePredateur,width=Largeur,height=Hauteur)
can3 = Canvas(ModeleProiePredateur,width=Largeur,height=Hauteur)

def affichage_page(page):
    if page == 1 :
        page1()
    if page == 2:
        page2()
    if page == 3 :
        page3()

def candel(cannum):
    if cannum == 1:
        Canvas.destroy(can1) #.destroy ne convient pas pour pouvoir reouvrir apres
    if cannum==2:
        Canvas.destroy(can2) #.delete de fonctionne pas comme prevu
    if cannum==3:
        Canvas.destroy(can3)

def pagesuivante():
    global x
    candel(x)
    x = x+1
    affichage_page(x)

def pageprecedente():
    global x
    y=x
    candel(y)
    y=y-1
    affichage_page(y)

def page1():

    can1.pack()
    planche = PhotoImage(file='/Users/loysforget1/Desktop/Planches/planche1.png')
    icon1 = can1.create_image(0,0, anchor='nw',image=planche)
    can1.photo = planche
    button1 = Button(ModeleProiePredateur, text = "Suivant", command = pagesuivante, anchor = N)
    button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
    button1_ModeleProiePredateur = can1.create_window(10, 730, anchor = SW, window=button1)

    button2 = Button(ModeleProiePredateur, text = "Precedent", command = pageprecedente, anchor = N)
    button2.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
    button2_ModeleProiePredateur = can1.create_window(100, 730, anchor = SW, window=button2)

    self.start()

def page2():

    can2.pack()
    planche = PhotoImage(file='/Users/loysforget1/Desktop/Planches/planche7.png')
    icon2 = can2.create_image(0,0, anchor='nw',image=planche)
    can2.photo = planche
    button1 = Button(ModeleProiePredateur, text = "Suivant", command = pagesuivante, anchor = N)
    button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
    button1_ModeleProiePredateur = can2.create_window(10, 730, anchor = SW, window=button1)

    button2 = Button(ModeleProiePredateur, text = "Precedent", command = pageprecedente, anchor = N)
    button2.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
    button2_ModeleProiePredateur = can2.create_window(100, 730, anchor = SW, window=button2)

def page3():

    can3.pack()
    planche = PhotoImage(file='/Users/loysforget1/Desktop/Planches/planche9.png')
    icon3 = can3.create_image(0,0, anchor='nw',image=planche)
    can3.photo = planche
    button1 = Button(ModeleProiePredateur, text = "Suivant", command = pagesuivante, anchor = N)
    button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
    button1_ModeleProiePredateur = can3.create_window(10, 730, anchor = SW, window=button1)

    button2 = Button(ModeleProiePredateur, text = "Precedent", command = pageprecedente, anchor = N)
    button2.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
    button2_ModeleProiePredateur = can3.create_window(100, 730, anchor = SW, window=button2)

Button(ModeleProiePredateur, text ='Page 1', command = page1).pack(side=RIGHT,padx = 5,pady = 5)

ModeleProiePredateur.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • I strongly suggest you take a look the the accepted answer to the question [**Switch between two frames in tkinter**](https://stackoverflow.com/a/7557028/355230) because it illustrates a `tkinter`-based software architecture that would probably be ideal for your application. – martineau Apr 14 '18 at 14:53

1 Answers1

0

You don't need to keep destroying and recreating each page. Create them once, then just switch them out.

Start by removing the pack statement from each page. The page should not be responsible for putting itself in its parent.

Next, create all the pages at the start of your program. Or, set each page to None, and when it's time to display the page first check to see if it's None or not. If it's None, create it before showing it.

Third, make your code that moves between pages be responsible for hiding the current page and showing the new page.

For example, your affichage_page function might look something like this:

def affichage_page(page):
    global current_page
    if current_page is not None:
        current_page.pack_forget()
        current_page = None

    if page == 1 :
        current_page = can1
    if page == 2:
        current_page = can2
    if page == 3 :
        current_page = can3

    current_page.pack(fill="both", expand=True)

Of course, you could be more efficient by storing the pages in a list so that you can using the page number as an index rather than using a set of if statements.

You then just need to create your pages before starting mainloop:

...
current_page = None
page1()
page2()
page3()
affichage_page(1)

ModeleProiePredateur.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685