프레임 내부에서 독립적으로 기하학을 관리하는 예제를 원했고 아래 코드를 작성하여 체크 무늬 깃발 모양 GUI를 작성했습니다.행 번호를 전달해야하는 이유는 무엇입니까?
수입은 Tkinter TK
# a class that has 2 columns of frames inside
class TwoFrames(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
# creates 2 frame objects and passes self as parent, which
# means object created using TwoFrames class is the parent
self.frame1 = tk.Frame(self)
self.frame2 = tk.Frame(self)
#manages 2 frames geometry
self.frame1.grid(column=0, sticky="nsew")
self.frame2.grid(column=1, sticky="nsew")
# enables resizing for 0th row, and 1st and 2nd columns of an
# object of this class
tk.Grid.rowconfigure(self, 0, weight=1)
tk.Grid.columnconfigure(self, 0, weight=1)
tk.Grid.columnconfigure(self, 1, weight=1)
class TwoLabels(tk.Frame):
def __init__(self, master=None, color=True):
super().__init__(master)
#creates 2 Label objects with TwoLabels object as parent
self.label1 = tk.Label(self)
self.label2 = tk.Label(self)
# configures the background color of labels for demonstrative
# purposes
if color:
#label1 will have red color
self.label1.configure(bg="black")
#label2 will have blue color
self.label2.configure(bg="white")
else:
#label1 will have blue color
self.label1.configure(bg="white")
#label2 will have red color
self.label2.configure(bg="black")
# manages the geometry
self.label1.grid(row=0, sticky="nsew")
self.label2.grid(row=1, sticky="nsew")
# enables resizing like above, but this time for 2 rows and 1
# column
tk.Grid.rowconfigure(self, 0, weight=1)
tk.Grid.rowconfigure(self, 1, weight=1)
tk.Grid.columnconfigure(self, 0, weight=1)
# creates the mainWindow
mainWindow = tk.Tk()
# creates a mainFrame that has 2 frames in it
mainFrame = TwoFrames(mainWindow)
# manages geometry of mainFrame and display it
mainFrame.pack(fill="both", expand=True)
# creates row_labels1 and row_labels2, both has 2 colored labels inside
row_labels1 = TwoLabels(mainFrame.frame1, True)
row_labels2 = TwoLabels(mainFrame.frame2, False)
# manages geometry of labels inside frames and displays them
row_labels1.pack(fill="both", expand=True)
row_labels2.pack(fill="both", expand=True)
# run the application
mainWindow.mainloop()
그러나 아이러니하게도, 코드 대신 내가 할 노력하고있어하는 것은 가능하지 않다 것처럼, 두 번째 행에의 한 수직 반이있는 체크 무늬 깃발을 제작있다. 나중에 나는 그것을 바꿨다.
#manages 2 frames geometry
self.frame1.grid(row=0, column=0, sticky="nsew")
self.frame2.grid(row=0, column=1, sticky="nsew")
와
는#manages 2 frames geometry
self.frame1.grid(column=0, sticky="nsew")
self.frame2.grid(column=1, sticky="nsew")
부분과 내가 처음 의도 한대로 일했다. 나는 그것이 효과가있어 기쁘다. 그러나. 형상은 적어도 클래스 기초에 대한 관리 여부되는 경우
는 잘 모르겠어요. 그렇지?
어쨌든 내가 통과 한 것과 같다고 가정하면 통과 번호는 행 번호가 변경됩니까?
내 코드를 Code Review에서 검토 할 수 있으면 기쁠 것입니다.
TwoFrames 클래스에서 0부터 시작하므로 0으로 가정 했으므로 행 번호를 전달하지 않아도된다고 가정했습니다. 나는 당신이 그들이 동일하다는 것을 말하고있는 동일한 가정을했다. 그러나 그것은 분명히 다르게 작동합니다. 즉, 어떻게 든 그들이 동일하지 않다는 것을 의미합니다. 나는 그것들 (당신이 똑같이 말하고있는 두 개의 그리드 옵션)이 어떻게 다른지 궁금합니다. – Nae
@Nae : 첫 번째 값의 기본값은 0이지만 두 번째 값의 기본값은 1입니다. 두 표 호출이 모두 0 인 행을 사용한다고 가정하지만 그렇지 않습니다. 'grid'를 호출 할 때마다 명시 적으로 행을 설정하지 않으면 행이 증가합니다. –
Ah 코드에서 차이점을 알지 못했습니다. 죄송합니다. 그렇다면 TwoLabels 클래스 함수는 원래 의도 한대로 어떻게 생깁니 까? 형상은 비슷하게 작성되었지만 이번에는 넘버가 전달되지 않았습니까? – Nae