파이썬(Python)/tkinter

[Python]tkinter 그리드 빈 칸 만들기(Grid, Space, Blank)

끄적끄적아무거나 2021. 12. 14. 09:37
반응형

 

목차

     

     

     

     

     

     

    [Python]tkinter 그리드 빈 칸 만들기: 함수 정의 설명

     

    grid 사용법에 대해 알고 싶으시면 아래 링크를 참조 하시길 바랍니다. 

     

    https://scribblinganything.tistory.com/293

     

    파이썬 tkinter - grid란? (간격, 배치, 정렬 방법) 예제로 쉽게 이해하기

    Python tkinter grid란? grid는 우리가 일반적으로 사용하는 엑셀처럼 행(row)과 열(column)의 번호를 이용해서 text, label, button 을 배치하는 작업이다. 아래 예제 작업에서 버튼으로 예제를 시행할 것이다..

    scribblinganything.tistory.com

     

     

    grid는 기본적으로 column 과 row의 번호를 설정하여 지정 합니다.

     

    btn_0 = Button(app, text="(0, 1)", width=10)
    btn_0.grid(column=1, row=0)
    
    btn_2 = Button(app, text="(0, 3)", width=10)
    btn_2.grid(column=3, row=0)

     

    예를 들어 위와 같이 작성하면 column=2, row=0에 해당하는 위치가 자동으로 없어지고 채워 집니다. 사이에 빈공간을 채우기 위해 아래와 같은 함수를 사용 합니다. 

     

     

    • grid_columnconfigure(column번호, minsize=최소크기)
    • grid_rowconfigure(row번호, minsize=최소크기)

     

     

    채우고 싶은 열과 행의 번호를 입력하고 최소 크기를 지정하면 만일 해당 열과 행에 값이 없더라도 최소 값을 채우게 됩니다.

     

     

     

     

     

    [Python]tkinter 그리드 빈 칸 만들기: 예제로 이해하기

     

    빈칸없이 채울 경우 코드>>

    from tkinter import *
    app = Tk()
    btn_0 = Button(app, text="(0, 1)", width=10)
    btn_0.grid(column=1, row=0)
    btn_1 = Button(app, text="(0, 2)", width=10)
    btn_1.grid(column=2, row=0)
    btn_2 = Button(app, text="(0, 3)", width=10)
    btn_2.grid(column=3, row=0)
    btn_3 = Button(app, text="(1, 1)", width=10)
    btn_3.grid(column=1, row=1)
    btn_4 = Button(app, text="(1, 2)", width=10)
    btn_4.grid(column=2, row=1)
    btn_5 = Button(app, text="(1, 3)", width=10)
    btn_5.grid(column=3, row=1)
    
    app.title('scribblinganything.tistory.com')
    app.mainloop()

     

    결과>>

     

    주석>> 

    빈칸을 만들지 않고 채울 경우 위처럼 6개의 버튼을 총 3열(Column) 2행(Row)로 채웠습니다. 

     

     

     

     

    2열(Column) 버튼 없앨 경우 코드>>

    from tkinter import *
    app = Tk()
    btn_0 = Button(app, text="(0, 1)", width=10)
    btn_0.grid(column=1, row=0)
    # btn_1 = Button(app, text="(0, 2)", width=10)
    # btn_1.grid(column=2, row=0)
    btn_2 = Button(app, text="(0, 3)", width=10)
    btn_2.grid(column=3, row=0)
    btn_3 = Button(app, text="(1, 1)", width=10)
    btn_3.grid(column=1, row=1)
    # btn_4 = Button(app, text="(1, 2)", width=10)
    # btn_4.grid(column=2, row=1)
    btn_5 = Button(app, text="(1, 3)", width=10)
    btn_5.grid(column=3, row=1)
    
    app.title('scribblinganything.tistory.com')
    app.mainloop()

     

     

    결과>>

     

     

    주석>>

    코드에 2열에 해당하는 버튼을 주석 처리하니 위 그림과 같이 2열의 버튼이 없어지고 빈칸을 매우고 2행 2열처럼 구성되었습니다.

    반응형

     

     

     

     

    2열(Column) 버튼 빈칸으로 채울 경우 코드>>

    from tkinter import *
    app = Tk()
    btn_0 = Button(app, text="(0, 1)", width=10)
    btn_0.grid(column=1, row=0)
    # btn_1 = Button(app, text="(0, 2)", width=10)
    # btn_1.grid(column=2, row=0)
    btn_2 = Button(app, text="(0, 3)", width=10)
    btn_2.grid(column=3, row=0)
    btn_3 = Button(app, text="(1, 1)", width=10)
    btn_3.grid(column=1, row=1)
    # btn_4 = Button(app, text="(1, 2)", width=10)
    # btn_4.grid(column=2, row=1)
    btn_5 = Button(app, text="(1, 3)", width=10)
    btn_5.grid(column=3, row=1)
    
    app.grid_columnconfigure(2, minsize=50)
    
    app.title('scribblinganything.tistory.com')
    app.mainloop()

    16번 라인: 2열에 10 크기로 빈칸을 채웠습니다.

     

     

    결과>>

     

    주석>>

    위 그림과 같이 2번째 콜롬을 빈칸으로 채웠습니다.

     

     

    반응형