2016-01-21 47 views

回答

0

我写了一些代码,它隐藏/显示一个图像点击一个按钮。您可以编辑它以适应您的需求。

注:目前,我使用pack(),并且pack_forget(),如果你想使用电网或地方,你必须使用grid_forget()place_forget()

进口的Tkinter

def hideBG(): 
    global state 
    if state == "Hidden": 
     background_label.pack() 
     state = "Showing" 

    elif state == "Showing": 
     background_label.pack_forget() 
     state = "Hidden" 




window = tkinter.Tk() 

background_image=tkinter.PhotoImage(file="BG.png") 
background_label = tkinter.Label(window, image=background_image) 

hideBttn = tkinter.Button(window, text="Hide Background", command=hideBG) 
state = "Showing" 

hideBttn.pack() 
background_label.pack() 

window.mainloop() 

这将创建一个标签内的图像和按钮。该按钮将获取图像的当前“状态”(无论是隐藏还是显示),并在按下按钮时调用hideBG函数将其更改为相反。

希望这会有所帮助!