2017-10-24 5 views
0

Rainbox Six Siege에 대한 임의의 연산자 이름 생성기를 프로그래밍했으며 이름이 올라 왔을 때 연산자 그림을 표시합니다. 이미지는 잘 보이지만 사라지지 않을 것입니다. 내가 필요로하는 모든이 나타나는 일단 사진을 제거하는 방법을 알고있다, 이것은 여전히 ​​WIP입니다 :tkinter에서 이미지가 사라지게 만드는 방법

from tkinter import * 
    import tkinter 
    import random 

    names = ['Sledge','Thatcher','Ash','Thermite','Twitch','Montagne','Glaz','Fuze','Blitz','IQ','Buck','Blackbeard','Capitão','Hibana'] 
    name = ["Smoke","Mute","Castle","Pulse","Doc","Rook","Kapkan","Tachanka","Jäger","Bandit","Frost","Valkyrie","Caveira","Echo"] 
    root = tkinter.Tk() 
    def pickName(): 
     rad = random.choice(names) 
     photo = PhotoImage(file=rad+".png") 
     label = Label(image=photo) 
     label.image = photo # keep a reference! 
     label.pack() 
     nameLabel.configure(text=rad, foreground="white", background="blue") 
     root.configure(background='blue') 
    def pickName1():  nameLabel.configure(text=random.choice(name),background="orange",foreground="black") 
     root.configure(background='orange') 



    root.title("Operator Picker") 

    root.geometry("250x100") 

    nameLabel = tkinter.Label(root, text="", font=('Helvetica', 32)) 
    nameLabel.pack() 
    Grid() 

    f1 = tkinter.Frame(root, height=100, width=100) #defines frame size in 
    pixels 
    f1.pack(side=tkinter.LEFT) #packs on the left 
    f1.pack_propagate(0) #tells frame not to let children control size 
    pickButton1 = tkinter.Button(f1, command=pickName, text="Pick       
    Attack",background="blue",foreground="white") 
    pickButton1.pack(fill=tkinter.BOTH, expand=1) #takes up all available space 

    f2 = tkinter.Frame(root, height=100, width=100) 
    f2.pack(side=tkinter.RIGHT) 
    f2.pack_propagate(0) 
    pickButton2 = tkinter.Button(f2, command=pickName1, text="Pick 
    Defend",background="orange",foreground="black") 
    pickButton2.pack(fill=tkinter.BOTH, expand=1) 

    root.mainloop() 

참고 :이 내 코드입니다. 두 개 이상의 이미지가 나타날 때의 모습입니다. https://imgur.com/eroXLLn

+0

당신도 기존의 위젯이 사라 만들기 위해 노력하고 어디 어디에서나 볼 수 없습니다. 당신은 그것을 파괴하지 않으며 당신은 그것을 숨기지 않습니다. –

+0

@BryanOakley 나는 그가 어떻게하는지 모른다고 생각한다. – Nae

+0

@BryanOakley 예 내 문제를 해결하는 방법을 모르겠다. –

답변

2

해당 기능을 호출 할 때마다 새 Label이 추가됩니다. 대신, 레이블을 한 번만 만들어야합니다 (아마 초기화 단계에서) 그림을 업데이트해야합니다. nameLabel의 텍스트를 업데이트하는 것과 마찬가지로 참조를 유지하는 단계입니다.

photo_label = tkinter.Label() 
def pickName(): 
    rad = random.choice(names) 
    photo = PhotoImage(file=rad+".png") 
    photo_label.configure(image = photo) 
    photo_label.image = photo # keep a reference! 
    photo_label.pack() 

    nameLabel.configure(text=rad, foreground="white", background="blue") 

및 전체 코드가 보일 것 같은 :

from tkinter import * 
import tkinter 
import random 

names = ['Sledge','Thatcher','Ash','Thermite','Twitch','Montagne','Glaz','Fuze','Blitz','IQ','Buck','Blackbeard','Capitão','Hibana'] 
name = ["Smoke","Mute","Castle","Pulse","Doc","Rook","Kapkan","Tachanka","Jäger","Bandit","Frost","Valkyrie","Caveira","Echo"] 
root = tkinter.Tk() 
photo_label = tkinter.Label() 
def pickName(): 
    rad = random.choice(names) 
    photo = PhotoImage(file=rad+".png") 
    photo_label.configure(image = photo) 
    photo_label.image = photo # keep a reference! 
    photo_label.pack() 

    nameLabel.configure(text=rad, foreground="white", background="blue") 
    root.configure(background='blue') 
def pickName1():  nameLabel.configure(text=random.choice(name),background="orange",foreground="black") 
root.configure(background='orange') 



root.title("Operator Picker") 

root.geometry("250x100") 

nameLabel = tkinter.Label(root, text="", font=('Helvetica', 32)) 
nameLabel.pack() 
Grid() 

f1 = tkinter.Frame(root, height=100, width=100) #defines frame size inpixels 
f1.pack(side=tkinter.LEFT) #packs on the left 
f1.pack_propagate(0) #tells frame not to let children control size 
pickButton1 = tkinter.Button(f1, command=pickName, text="PickAttack",background="blue",foreground="white") 
pickButton1.pack(fill=tkinter.BOTH, expand=1) #takes up all available space 

f2 = tkinter.Frame(root, height=100, width=100) 
f2.pack(side=tkinter.RIGHT) 
f2.pack_propagate(0) 
pickButton2 = tkinter.Button(f2, command=pickName1, text="PickDefend",background="orange",foreground="black") 
pickButton2.pack(fill=tkinter.BOTH, expand=1) 

root.mainloop() 
+0

photo_label이 정의되어 있지 않습니다. –

+0

@AidanChristopher 자신의 코드 조각에서'photo_label'은'label'라고 생각합니다. – Nae

+0

@AidanChristopher 나는이 접근법이 훨씬 좋다고 생각한다. 기본적으로'nameLabel'을 사용하여 수행하는 작업이므로 일관성이 있습니다. 이 대답의'def pickName() :'앞에'photo_label = tkinter.Label()'을 추가하면됩니다. – Nae