1
我正在構建一個小的GUI應用程序,一旦點擊一個按鈕,一個新的頂級窗口將打開並且它應該顯示按鈕圖像。添加一個圖像按鈕到頂層窗口Tkinter
我可以讓圖像按鈕在根窗口上工作,但不在頂層窗口上。只有黑盒出現。
我在這兩個窗口上都有一個通用按鈕,它們可以工作。
我是Python新手。
import Tkinter
from Tkinter import *
from PIL import ImageTk, Image
root = Tkinter.Tk()
root.title("First Window")
root.configure(background = "black")
def new_window():
win2 = Toplevel(root)
win2.geometry("650x350+50+40")
win2.title("Second Window!")
win2.configure(background = "white")
def close1():
win2.destroy()
img1 = ImageTk.PhotoImage(Image.open("./images/close.gif"))
c1 = Button(win2, image = img1, bg ="black", command = close1)
c1.grid(row = 1)
c2= Tkinter.Button(win2, text='close', command = close1)
c2.grid(row = 2)
nw = Tkinter.Button(root, text = 'New Window' , command = new_window)
nw.grid(row = 1)
def close3():
root.destroy()
img3 = ImageTk.PhotoImage(Image.open("./images/close.gif"))
c3 = Button(root, image = img3, bg ="black", command = close3)
c3.grid(row = 2)
root.mainloop()