2017-12-29 45 views
-2

나는 스도쿠 퍼즐 생성기를위한 9x9 격자를 만들었습니다. 지금까지 프레임의 첫 번째 행에 대해 9 개의 변수를 작성했으며 9 개의 변수 그리드 행/열을 지정했습니다. 내가 사용하고있는 코드는 다음과 같습니다.9x9 그리드를보다 효율적으로 만드는 방법은 무엇입니까? (Python, Tkinter)

그리드에 대해 81 개의 셀을 만들어야하기 때문에보다 효율적인 방법이 필요합니다. 어떻게하면보다 효율적으로/더 잘할 수 있습니까? 도와주세요!

번호 :

from tkinter import * 

root = Tk() 
root.title('Sudoku') 
root.geometry('1000x1000') 

# create all of the main containers 
center = Frame(root, bg='white', width=900, height=900, padx=3, pady=3) 

# layout all of the main containers 
root.grid_rowconfigure(9, weight=1) 
root.grid_columnconfigure(9, weight=1) 
center.grid(row=1, sticky="nsew") 

# create the center widgets 
center.grid_rowconfigure(0, weight=1) 
center.grid_columnconfigure(1, weight=1) 

cell1 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 
cell2 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 
cell3 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 
cell4 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 
cell5 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 
cell6 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 
cell7 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 
cell8 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 
cell9 = Frame(center, bg='white', highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, padx=3, pady=3) 

#create first row of grid 

cell1.grid(row=0, column=0) 
cell2.grid(row=0, column=1) 
cell3.grid(row=0, column=2) 
cell4.grid(row=0, column=3) 
cell5.grid(row=0, column=4) 
cell6.grid(row=0, column=5) 
cell7.grid(row=0, column=6) 
cell8.grid(row=0, column=7) 
cell9.grid(row=0, column=8) 

root.mainloop() 
+1

for 루프를 사용 하시겠습니까? 'i in range (81) : – Novel

+3

좋은 질문이지만 다른 곳에서도 훌륭한 답이 있습니다. 중첩 목록 (Python) 또는 2 차원 배열 (tkinter)에 대한 자습서를 검색하십시오. Stack Overflow는 정액 프로그래밍 주제에 대한 자습서를위한 장소가 아닙니다. – Prune

답변

1

행하기위한 루프 및 컬럼 루프. 사전에 위젯 스토어 참조 : 위의와

cells = {} 
for row in range(9): 
    for column in range(9): 
     cell = Frame(center, bg='white', highlightbackground="black", 
        highlightcolor="black", highlightthickness=1, 
        width=100, height=100, padx=3, pady=3) 
     cell.grid(row=row, column=column) 
     cells[(row, column)] = cell 

, 당신은 cells DICT를 통해 어떤 위젯을 참조 할 수 있습니다. 예를 들어 행 3, 열 4 (0부터 셀 수)의 셀을 빨간색으로 설정하려면 다음을 수행 할 수 있습니다.

+0

시도했지만 구문 오류가 발생했습니다 –

+0

'cells = dict()'는이 특별한 경우'cells = list() '보다 우월합니까? 나는 보통'grid' 레이아웃이 사용될 때 list를 사용하는 것이 더 쉽다는 것을 알게된다. – Nae

+0

@ Nae : 모든 우월성은 순전히 주관적입니다. 개인적으로 사전 목록을 목록보다 더 쉽게 사용할 수 있습니다. –