ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python: Window programming Tkinter 라이브러리 정리 1
    Python 2021. 4. 9. 15:44

    < Tkinter란? >

     

    < Tkinter 라이브러리 구성요소 >

     

    1. 윈도창 조절

    from tkinter import *
    
    # Tk객체 생성
    window =Tk()
    window.title("연습하기")
    
    # 기본창 사이즈
    window.geometry("400x100")
    
    # 창 크기 조절하기
    window.resizable(width=True, height=True)
    
    # 윈도 창 띄우기
    window.mainloop()
    

     

     

    2. Label을 이용하여 문자 표현하기

    from tkinter import *
    window = Tk()
    label1 = Label(window, text="abcd")
    label2 = Label(window, text="efgh", font=("궁서체", 30), fg="blue")
    label3 = Label(window, text= "ijkl", bg="magenta", width=20, height=5, anchor=SE)
    # anchor를 통해 문자의 위치를 고정
    
    label1.pack()
    label2.pack()
    label3.pack()
    # 해당 레이블을 표시하기
    
    window.mainloop()

     

     

    3. Label을 이용하여 이미지 표현하기

    from tkinter import *
    window = Tk()
    photo = PhotoImage(file="gif/lollipop.gif")
    label1 = Label(window, image=photo)
    label1.pack()
    window.mainloop()

     

     

    4. 버튼 위젯 사용하기

    from tkinter import *
    window = Tk()
    btn1 = Button(window, text="파이썬 종료", fg="red",
                  command=quit)
    btn1.pack()
    window.mainloop()

     

     

    5. 메시지 박스 사용하기

    from tkinter import *
    from tkinter import messagebox
    
    def myFunc():
        messagebox.showinfo(title="클릭하기", message="클릭되었네?")
    
    window = Tk()
    btn = Button(window, text="클릭해보세요 ", command=myFunc)
    btn.pack()
    window.mainloop()

     

     

    6. 체크버튼 사용하기

    from tkinter import *
    from tkinter import messagebox
    window =Tk()
    
    def myFunc():
        if chk.get() == 0: # 체크가 꺼져있으면
            messagebox.showinfo("", "체크 버튼이 꺼져있어요")
        else:  # 체크가 켜져있으면
            messagebox.showinfo("", "체크 버튼이 켜졌어요")
    
    chk = IntVar() # 체크값을 넣어둘 int형 변수 생성
    cb = Checkbutton(window, text= "클릭하세요", variable = chk,
                     command=myFunc)
    cb.pack()
    window.mainloop()

     

     

    7. 라디오 버튼 사용하기

    from tkinter import *
    window=Tk()
    
    def myFunc():
        if var.get() == 1:
            label.configure(text="강아지")
            # configure을 이용해서 label에 새로운 값을 지정해준다.
    
        if var.get() == 2:
            label.configure(text="고양이")
    
        if var.get() == 3:
            label.configure(text="토끼")
    
    var = IntVar()
    
    rb1 = Radiobutton(window, text="강아지", variable=var, value=1, command=myFunc)
    rb2 = Radiobutton(window, text="고양이", variable=var, value=2, command=myFunc)
    rb3 = Radiobutton(window, text="토끼", variable=var, value=3, command=myFunc)
    
    label = Label(window, text="선택: ", fg="red")
    
    rb1.pack()
    rb2.pack()
    rb3.pack()
    
    label.pack()
    
    window.mainloop()

     

     

    8. 위젯 위치 지정하기

    from tkinter import *
    window = Tk()
    
    btn1 = Button(window, text="버튼1")
    btn2 = Button(window, text="버튼2")
    btn3 = Button(window, text="버튼3")
    
    btn1.pack(side=LEFT)
    btn2.pack(side=LEFT)
    btn3.pack(side=LEFT)
    # 인자에 LEFT값을 줌으로써 위젯을 왼쪽으로 붙인다. 
    
    window.mainloop()

     

     

     

     

    from tkinter import *
    window = Tk()
    btnList = list()
    
    for i in range(10):
        btnList.append(Button(window, text="버튼" + str(i+1)))
    
    for btn in btnList:
        btn.pack(side=RIGHT)
    
    window.mainloop()

     

     

     

     

    from tkinter import *
    window = Tk()
    btnList = list()
    
    for i in range(3):
        btnList.append(Button(window, text="버튼" + str(i+1)))
    
    for btn in btnList:
        btn.pack(side=TOP, fill=X)
        # fill 인자에 X를 줌으로써 해당 창을 채운다.
    
    window.mainloop()

     

     

    9. padding과 margin 주기

    from tkinter import *
    window = Tk()
    btnList = list()
    
    for i in range(3):
        btnList.append(Button(window, text="버튼" + str(i+1)))
    
    for btn in btnList:
        btn.pack(side=TOP, fill=X, ipadx=10, ipady=10)
    
    window.mainloop()

     

     

     

     

    from tkinter import *
    window = Tk()
    btnList = list()
    
    for i in range(3):
        btnList.append(Button(window, text="버튼" + str(i+1)))
    
    for btn in btnList:
        btn.pack(side=TOP, fill=X, padx=10, pady=10)
    
    window.mainloop()

     

     

    10. 위젯을 고정위치에 배치하기

    from tkinter import *
    window = Tk()
    btnList = list()
    
    for i in range(9):
        btnList.append(Button(window, text="버튼" + str(i+1)))
    
    x = 0
    y = 0
    
    for i in range(3):
        x = 0
        for j in range(3):
            btnList[i*3+j].place(x=x, y=y, width=50, height=30)
            x += 50
        y += 30
    
    window.mainloop()

     

     

     

     

    from tkinter import *
    import random
    SIZE = 80
    photos = ['eclair', 'froyo', 'gingerbread', 'honeycomb',
    'jellybean', 'kitkat', 'lollipop', 'marshmallow', 'nougat']
    
    random.shuffle(photos)
    window = Tk()
    window.geometry("{}x{}".format(SIZE*3, SIZE*3))
    
    x = 0
    y = 0
    photoList = [None for _ in range(9)]
    
    for i in range(9):
        photoList[i] = PhotoImage(file='gif/' + photos[i] + '.gif')
    
    for i in range(3):
        x = 0
        for j in range(3):
            btn = Button(window, image=photoList[i*3 + j])
            btn.place(x=x, y=y)
            x += SIZE
        y += SIZE
    
    window.mainloop()

     

    반응형

    댓글

Designed by Tistory.