我正在創建遊戲,並且我正在使用tkinter來構建GUI。在tkinter的帆布上提升並提高帆布
在這個遊戲中,我想讓用戶從一個窗口開始,由一個畫布填充,圖像作爲背景,以及某些項目窗口中的某些按鈕。這個畫布是遊戲的家庭畫布。
問題是,我想讓用戶點擊一個按鈕登陸其他畫布,誰將承載其他東西,如地圖或其他行爲的其他按鈕。實際上,我想疊加幾個畫布(如在Z-index方法中),並在列表頂部放置一個畫布,當我需要時(例如,如果單擊按鈕)。
我已經搜索過,我不得不多次改變主意,現在我真的不知道該怎麼做。我發現下面的代碼在這裏堆棧溢出,但它是編碼爲Python 2(我認爲),我開始在Python 3編碼,所以我不能將它翻譯成Python 3並解決我的問題問題。
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frame = tk.Frame(self)
self.frame.pack(side="top", fill="both", expand=True)
self.label = tk.Label(self, text="Hello, world")
button1 = tk.Button(self, text="Click to hide label",
command=self.hide_label)
button2 = tk.Button(self, text="Click to show label",
command=self.show_label)
self.label.pack(in_=self.frame)
button1.pack(in_=self.frame)
button2.pack(in_=self.frame)
def show_label(self, event=None):
self.label.lift(self.frame)
def hide_label(self, event=None):
self.label.lower(self.frame)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
我的代碼使用格:
from tkinter import *
fenetre = Tk()
fenetre.title(my game)
# acceuil
# variable acceuil
largeur = 700
hauteur = 430
BG_acceuil = PhotoImage(file="BG_acceuil.gif")
acceuil = Canvas(fenetre, width=largeur, height=hauteur)
acceuil.create_image(0, 0, anchor=NW, image=BG_acceuil)
acceuil.grid(row=0)
acceuil.pack()
# fond
fond = PhotoImage(file="BG_acceuil.gif")
acceuil2 = Canvas(fenetre, width=largeur, height=hauteur)
acceuil2.create_image(0, 0, anchor=NW, image=fond)
acceuil2.pack()
# variable bt_jouer
x0 = 80
y0 = 230
class hide_me():
def hide_me(event, widget, pos):
widget.grid_forget()
def show_me(event, widget, pos):
widget.grid(row=pos)
# Boutton jouer
BT_jouer = Button(acceuil, text="Jouer", command=hide_me())
BT_jouer.configure(width=10, activebackground="#33B5E5", relief=GROOVE)
BT_jouer_window = acceuil.create_window(x0, y0, window=BT_jouer,)
BT_jouer.bind('<Button-1>', lambda event: hide_me(event, BT_jouer, 1))
BT_jouer.grid(row=1)
# Bouton règle
BT_regle = Button(acceuil2, text="Règles", command=fenetre.destroy)
BT_regle.configure(width=10, activebackground="#33B5E5", relief=FLAT, bd=0)
BT_regle_window = acceuil2.create_window(x0, y0 + 50, window=BT_regle)
# Boutton quitter
BT_quit = Button(acceuil, text="Quitter", command=fenetre.destroy)
BT_quit.configure(width=10, activebackground="#33B5E5", relief=FLAT)
BT_quit_window = acceuil.create_window(x0, y0 + 100, window=BT_quit)
fenetre.mainloop()
您的意思是說,一旦上述對象被隱藏,我就無法與堆棧的最後一個對象進行交互了。在這種情況下,我至少不能讓他們正確嗎? 當你說一個對象可以被刪除和回想,它是否被破壞並重新創建?因爲,我試圖不摧毀任何物體。 – Djabadours
評論我讀的是,對象仍然在堆棧上,並且該pack()本身應該能夠恢復.pack_forget()應該能夠恢復它。我試了一下,但它不太正確。這可能是因爲pack()是順序的 - 它不存儲它的位置。嘗試grid(),您可以在其中指定實際的行位置。有一個grid_forget()隱藏和grid()應該恢復。 – jcoppens
編輯上述回覆:如果您真的知道它在哪裏,可以將「已忘記」小工具恢復到原始位置。 – jcoppens