我正在用Tkinter创建一本书,我已经将我的书的每个页面都定义为画布,我希望能够通过关闭当前画布(当前页面)并打开下一个画布(下一页)来在页面之间切换
我有两个问题:
问题在于,当我使用命令Canvas.delete(can1)
时,画布没有关闭(我不知道为什么......),所以我尝试了Canvas.destroy(can1)
,但随后当我想要转到上一页时,我不能因为我将其销毁(我认为这是问题所在)。是否有其他功能可以从我的主窗口中删除画布,但是如果我想重新打开它,我可以吗?
如果你查看代码(最后),我需要使用一个按钮来打开第一页,因为我无法将#1; page1()'在mainloop()中。是否有命令在程序开始时调用一次函数?因此,当我运行它时,第一页自动打开... (我希望你知道/了解我正在搜索的内容)。
我为我可怕的英语和代码中的法语道歉......;)
代码在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()
答案 0 :(得分:0)
您不需要继续销毁和重新创建每个页面。创建一次,然后将它们切换出来。
首先从每个页面中删除pack
语句。该页面不应该将自己置于其父级中。
接下来,在程序开头创建所有页面。或者,将每个页面设置为None
,并在首次显示页面时检查它是否None
。如果是None
,请在显示之前创建它。
第三,让你在页面之间移动的代码负责隐藏当前页面并显示新页面。
例如,您的affichage_page
函数可能如下所示:
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)
当然,通过将页面存储在列表中可以提高效率,这样您就可以使用页码作为索引,而不是使用一组if
语句。
然后您只需在开始mainloop
之前创建页面:
...
current_page = None
page1()
page2()
page3()
affichage_page(1)
ModeleProiePredateur.mainloop()