![](/img/trans.png)
[英]Closing current window when opening a new window in tkinter python
[英]Python Tkinter: How do I apply a new background image when opening a new tk window?
我使用下面的代碼(每個部分使用不同的變量名稱)為每個tkinter窗口創建背景圖像。 每一個都是在一個函數中啟動的,並且兩個都可以獨立正常工作。
但是,從另一個功能加載一個功能時,第二個功能則無法顯示圖像。 (我也嘗試導入每個功能中的所有相關內容)。 它在使用tk.destruct()的情況下有效,但是如果我想使其保持打開狀態,或使用對其隱藏。 withdraw(),圖像無法顯示,從而使第二個窗口無用。
background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
好的,我為您解決了一個問題。 基本上,您需要的是在第二個tk.Toplevel()
窗口中使用tk.Toplevel()
,並確保“ parent”為root2
以便圖像出現在第二個窗口中。
我為圖像使用了按鈕,您有標簽,所以您可能希望更改它,但是按鈕為我提供了一種輕松打開新tk窗口的方法,我還使用了.pack()
而不是.place()
,因為對我來說更快。 了解我在Windows上使用python 3.3可能對您有所幫助,因此您可能需要tkinter
的大寫tkinter
T
import tkinter as tk
root1 = tk.Tk()
def new_window():
root2 = tk.Toplevel()
# click the last button and all tk windows close
def shutdown():
root1.destroy()
root2.destroy()
background_image2 = tk.PhotoImage(file = '...')
background_button2 = tk.Button(root2, image = background_image2, command = shutdown)
background_button2.pack()
root2.mainloop()
background_image1 = tk.PhotoImage(file = '...')
# have used a button not a label for me to make another tk window
background_button1 = tk.Button(root1, image = background_image1, command = new_window)
background_button1.pack()
root1.mainloop()
@ user2589273下次您應該添加更多代碼,以便可以輕松地給出答案,並為您量身定制,只是一個建議。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.