2017-03-25 12 views
0

각 단추 (색상 목록 아래에 있음)를 클릭하면 이름에서 전체 배경색으로 변경됩니다. 나는 내가 16 라인의 코드의이 부분을 목표로 좀 것 같아요 :목록에서 각 단추 (색)를 배경색으로 바꾸십시오.

command=window.configure(background=c) 

그러나이 작동하지 않습니다 ... 내가 정말 여기에 약간의 도움이 감사하겠습니다.

당신은 함수에 색상 값을 캡슐화 functools.partial를 사용할 필요가
import tkinter 
window = tkinter.Tk() 

window.title("Colors") 
window.geometry('650x300') 
window.configure(background="#ffffff") 

#Create a list of colors 
colors = ['red', 'green', 'blue', 'cyan', 'orange', 'purple', 'white', 'black'] 

#loops through each color to make button 
for c in colors: 
    #create a new button using the text & background color 
    b = tkinter.Button(text=c, bg=c, font=(None, 15), command=(window.configure(background=c))) 
    b.pack() 

window.mainloop() 
+0

[ "bg"] = c? –

답변

0

(는 "폐쇄"라고도 함) : 여기에 전체 코드입니다.

from functools import partial 

for c in colors: 
    #create a new button using the text & background color 
    b = tkinter.Button(text=c, bg=c, font=(None, 15), command=partial(window.configure, background=c)) 
    b.pack() 
+0

정확히 내가 필요한 것! –

0

배경색을 변경하고 functools.partial을 사용하려면이 기능을 사용해야합니다.

from functools import partial 
import tkinter 
window = tkinter.Tk() 

window.title("Colors") 
window.geometry('650x300') 
window.configure(background="#ffffff") 

#Create a list of colors 
colors = ['red', 'green', 'blue', 'cyan', 'orange', 'purple', 'white', 'black'] 

def change_colour(button, colour): 
    window.configure(background=colour) 

#loops through each color to make button 
for c in colors: 
    #create a new button using the text & background color 
    b = tkinter.Button(text=c, bg=c, font=(None, 15)) 
    b.configure(command=partial(change_colour, b, c)) 
    b.pack() 

window.mainloop()