공식예제(Hello World!)
import tkinter as tk
class Application(tk.Frame): # 부모 tk.Frame
def __init__(self, master=None):
super().__init__(master) # 상속
self.master = master
self.pack() # GUI 달기
self.create_widgets() # 메소드 호출
def create_widgets(self): # 메소드 정의
self.hi_there = tk.Button(self) # 버튼 달기
self.hi_there["text"] = "Hello World\\n(click me)" # 문자넣기
self.hi_there["command"] = self.say_hi # 메소드 호출
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=self.master.destroy)
self.quit.pack(side="bottom")
def say_hi(self): # 메소드 정의
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
섭씨-화씨 변환기
from tkinter import *
def fa() : # 섭씨 -> 화씨
fatemp = float(e1.get())
fatemp = fatemp * 9 / 5 + 32
e2.insert(0, fatemp)
def ce() : # 화씨 -> 섭씨
cetemp = float(e2.get())
cetemp = (cetemp - 32) * 5 / 9
e1.insert(0, cetemp)
window = Tk() # 메인 윈도우 생성
window.title("TEMPERATURE") # 윈도우창 제목
window.geometry("500x100") # 윈도우창 크기 가로 500 세로 100
window.resizable(False, False) # 윈도우창 크기 조절X
l1 = Label(window, text = "섭씨") # 라벨 만들기
l2 = Label(window, text = "화씨")
l1.pack() # 라벨 달기
l2.pack()
l1.place(x = 150, y = 0) # 라벨 위치
l2.place(x = 150, y = 20)
e1 = Entry(window) # 텍스트 박스
e2 = Entry(window)
e1.pack()
e2.pack()
b1 = Button(window, text = "섭씨->화씨", command = fa) # fa 함수
b2 = Button(window, text = "화씨->섭씨", command = ce) # ce 함수
b1.pack()
b2.pack()
window.mainloop()