2013-03-21 3 views
0

나는 별도의 기능을 정의했으며 main() 기능을 가지고있다. main() 함수에는 클릭 할 때 다른 함수를 호출하는 여러 개의 버튼이 있습니다. 예.버튼을 연속으로 정렬하는 방법은 무엇입니까?

def main(): 
    global root 
    global tkinter 
    global canvas 
    root = Tk() 
    root.title("Shapes Quiz - Flow Computing") 
    Button(root, text ="1", bg="Grey", width=10, command=pentagon).pack() 
    Button(root, text ="2", bg="Grey", width=10, command=circle).pack() 
    Button(root, text ="3", bg="Grey", width=10, command=diamond).pack() 
    Button(root, text ="4", bg="Grey", width=10, command=hexagon).pack() 
    Button(root, text ="5", bg="Grey", width=10, command=triangle).pack() 
    Button(root, text ="6", bg="Grey", width=10, command=square).pack() 
    Button(root, text ="7", bg="Grey", width=10, command=rectangle).pack() 
    Button(root, text ="8", bg="Grey", width=10, command=heptagon).pack() 
    Button(root, text ="9", bg="Grey", width=10, command=octagon).pack() 
    Button(root, text ="10", bg="Grey", width=10, command=oval).pack() 
    Button(root, text ="Help", bg="Grey", width=10, command=help).pack() 
    Button(root, text ="Clear", bg="Red", width=10, command=clearshapes).pack() 
    Button(root, text ="QUIT", bg="Red", width=10, command=quit).pack() 

이러한 버튼을 구성하는 더 쉬운 방법이 있어야합니다. 저는 프레임과 그리드, 심지어 좌표의 사용에 대해서도 들었습니다. 그러나 나는 이것을하는 방법을 여전히 확신 할 수 없다.

나는 그들이 현재 나타나는대로 그들은 수평하지 수직을 그래서 버튼을 구성하고 싶습니다.

+1

다른 링크 몇 개 : http://effbot.org/tkinterbook/tkinter-index.htm http://effbot.org/tkinterbook/grid.htm – codeape

+0

수정되었지만이 자습서는 답변을 제공하지 않습니다. 찾고, 그것은 여러 개의 버튼을 정렬하는 방법을 말하지 않습니다. –

+0

codeape의 링크는 좀 더 많은 정보를 제공합니다. 읽을거야. –

답변

1

변경 pack()에서 pack(side='left')으로 변경하십시오.

+0

이 답변은 작동하지만, 오른쪽으로 캔버스를 찌그러 뜨릴 수 있습니다.이 형성에서 캔버스 아래쪽에 텍스트를 삽입 할 수있는 방법이 있습니까? –

+0

나는 그 사용을 실현했습니다. 아래쪽 은 모든 버튼이 아래쪽에 표시된다는 것을 의미하지만이 버튼을 적용하면 수직으로 돌아갑니다. –

+0

Nevermind, 프레임을 사용하여 이것을 알아 냈습니다. –

2

확실히,이 격자 형상 관리자와 함께이 작업을 수행 할 수있는 쉬운 방법 (링크가 코멘트에 이미)입니다 : 당신은 항상 거의 동일한 코드를 사용

# ... 
root.title("Shapes Quiz - Flow Computing") 
BUTTONS = [{"text":"1", "bg":"Grey", "width":10, "command":pentagon}, 
      {"text":"2", "bg":"Grey", "width":10, "command":circle}, 
      # Rest of the buttons 
      {"text":"QUIT", "bg":"Red", "width":10, "command":quit}] 
for i, options in enumerate(BUTTONS): 
    Button(root, **options).grid(row=0, column=i) 

주, 그래서 보일 수 있습니다 만약 당신이 for 루프를 사용하고 widget의 옵션을 미리 선언한다면,

관련 문제