Python-tkinter 기본 사용 방법

3358 단어

기본 사용 과정은 4단계로 나뉜다


1. tk 객체 만들기
2. 일부 위치를 설정하고 컨트롤을 만듭니다
3. 패키지나 플레이스 등 서로 다른 배치 기능을 이용하여 컨트롤을 관련 위치에 배치
4. 메시지 순환을 시작합니다. 그렇지 않으면 win을 표시할 수 없습니다.mainloop()
#tkinter
import tkinter
# 
str_var=None
def btn_click():
    s=str_var.get()
    print(s)
if __name__ == '__main__':
    # Tk , 
    win=tkinter.Tk()
    # 
    win.title("Tkinter ")
    # 
    win.geometry("800x600+100+100")#widthxheight+x+y
    width=800
    height=600
    # 
    screen_width=win.winfo_screenwidth()
    screen_height=win.winfo_screenheight()
    x=(screen_width-width)//2
    y=(screen_height-height)//2
    win.geometry("%dx%d+%d+%d"%(width,height,x,y))
    # 
    lab=tkinter.Label(master=win,text=' ',background='red')
    lab.pack(side=tkinter.LEFT)
    lab=tkinter.Label(master=win,text='2nd ')
    lab.pack()
    # 
    btn=tkinter.Button(master=win,text=' ',command=btn_click)#command= 
    btn.pack()
    # 
    str_var=tkinter.StringVar()
    entry=tkinter.Entry(master=win,width=100,textvariable=str_var)# =100,textvariable StringVar
    entry.pack()
    str_var.set("TOM")

    # 
    win.mainloop()

Eg. 간편한python 계산기

from  tkinter  import ttk
import tkinter
# 
str_var1=None
str_var2=None
str_var=None
def btn_click():
    s1=str_var1.get()
    s2=str_var2.get()
    if comboxlist.get()=='+':
        str_var.set(float(s1)+float(s2))
    elif comboxlist.get()=='-':
        str_var.set(float(s1) - float(s2))
    elif comboxlist.get()=='*':
        str_var.set(float(s1) * float(s2))
    elif comboxlist.get() == '/':
        str_var.set(float(s1) / float(s2))
    elif comboxlist.get() == '**':
        str_var.set(float(s1) ** float(s2))
def btn_clear():
    str_var1.set("")
    str_var2.set("")
    str_var.set("")

if __name__ == '__main__':
    # Tk , 
    win=tkinter.Tk()
    # 
    win.title(" ")
    width=400
    height=200
    # 
    screen_width=win.winfo_screenwidth()
    screen_height=win.winfo_screenheight()
    x=(screen_width-width)//2
    y=(screen_height-height)//2
    win.geometry("%dx%d+%d+%d"%(width,height,x,y))
    # 1

    str_var1 = tkinter.StringVar()
    entry = tkinter.Entry(master=win, width=10, textvariable=str_var1)
    entry.place(x=50,y=50,width=50,height=25)
    #  
    comvalue = tkinter.StringVar()
    comboxlist = ttk.Combobox(win, textvariable=comvalue)
    comboxlist["values"] = ("+", "-", "*", "/","**")
    comboxlist.current(0)
    comboxlist.place(x=110,y=50,width=50,height=25)
    # 2
    str_var2 = tkinter.StringVar()
    entry = tkinter.Entry(master=win, width=10, textvariable=str_var2)
    entry.place(x=170,y=50,width=50,height=25)
    # 
    btn = tkinter.Button(master=win, text='=', command=btn_click)  # command= 
    btn.place(x=230,y=50,width=50,height=25)


    # 3 
    str_var = tkinter.StringVar()
    entry = tkinter.Entry(master=win, width=10, textvariable=str_var)
    entry.place(x=290,y=50,width=50,height=25)

    #  
    btn = tkinter.Button(master=win, text=' ', command=btn_clear)  # command= 
    btn.place(x=330, y=150, width=50, height=25)


    win.mainloop()

좋은 웹페이지 즐겨찾기