# 9์ฅ Lab ๋์ ๋ฌธ์ ํ์ด
440p_Lab ๋์ ๋ฌธ์ )
from tkinter import *
# ๋ฒํผ์ ์ ํํ ์ ์๋์ง ์ฌ๋ถ ๊ฒ์ฌ --> ์ ํ ๊ฐ๋ฅํ๋ฉด o,x ํ์
def checked(i):
global player
button = list[i]
# ์ฌ์ฉ์๊ฐ ์ ํํ ์ ์๋ ๋ฒํผ์ ๋๋ฅผ ๊ฒฝ์ฐ ์๋ฌด ํ๋๋ ์ทจํ์ง ์๊ณ ๋ฆฌํด
if button["text"] != " ":
return
button["text"] = " " + player + " "
button["bg"] = "yellow"
if player == "X":
button["bg"] = "yellow"
else:
button["bg"] = "lightgreen"
# ์นํจ๊ฐ ๊ฐ๋ ธ๋์ง ํ์ธ
if (list[0]["text"] == list[1]["text"] == list[2]["text"] != " " or
list[3]["text"] == list[4]["text"] == list[5]["text"] != " " or
list[6]["text"] == list[7]["text"] == list[8]["text"] != " " or
list[0]["text"] == list[3]["text"] == list[6]["text"] != " " or
list[1]["text"] == list[4]["text"] == list[7]["text"] != " " or
list[2]["text"] == list[5]["text"] == list[8]["text"] != " " or
list[0]["text"] == list[4]["text"] == list[8]["text"] != " " or
list[2]["text"] == list[4]["text"] == list[6]["text"] != " "):
# ์นํจ๊ฐ ๊ฐ๋ฆฌ๋ฉด ์น์ ์ถ๋ ฅ
message = player + " ์น๋ฆฌ!"
for b in list:
b["state"] = "disabled"
status.config(text=message)
if player == "X":
player = "O"
else:
player = "X"
elif all(button["text"] != " " for button in list):
# ๋น๊ธด ๊ฒฝ์ฐ
message = "๋น๊ฒผ์ต๋๋ค."
status.config(text=message)
else:
# ์ด์ ๋๊ตฌ ์ฐจ๋ก์ธ์ง ์ถ๋ ฅ
if player == "X":
player = "O"
else:
player = "X"
message = player + "์ ์ฐจ๋ก์
๋๋ค."
status.config(text=message)
window = Tk() # ์๋์ฐ ์์ฑ
player = "X" # ์์ ํ๋ ์ด์ด๋ x
list = []
for i in range(9):
b = Button(window, text=" ", command=lambda k=i: checked(k))
b.grid(row=i // 3, column=i % 3)
list.append(b)
status = Label(window, text="x์ ์ฐจ๋ก์
๋๋ค.", font=("Helvetica", 14))
status.grid(row=3, column=0, columnspan=3)
window.mainloop()
440p_Lab ๋์ ๋ฌธ์ )
from tkinter import *
window = Tk()
colors = ['#000000', '#FFC0CB', '#800080', '#FFFF00', '#0000FF', '#808080']
r = 0
for c in colors:
Label(window, text=c, relief=RIDGE, width=15).grid(row=r, column=0)
Button(window, bg=c, width=10).grid(row=r, column=1)
r = r + 1
window.mainloop()
453p_Lab ๋์ ๋ฌธ์ )
from tkinter import *
import random
answer = random.randint(1, 100)
tries = 0
max_tries = 10
def guessing():
global tries
guess = int(guessField.get())
if guess > answer:
msg = "๋์!"
elif guess < answer:
msg = "๋ฎ์!"
else:
msg = "์ ๋ต!"
tries += 1
if tries >= max_tries:
msg = "\n๊ฒ์ ์ข
๋ฃ! ์๋ ํ์๋ฅผ ์ด๊ณผํ์ต๋๋ค."
tryButton.config(state=DISABLED)
triesLabel["text"] = f"์๋ ํ์: {tries}/{max_tries}"
resultLabel["text"] = msg
guessField.delete(0, 5)
def reset():
global answer, tries
answer = random.randint(1, 100)
tries = 0
triesLabel["text"] = f"์๋ ํ์: {tries}/{max_tries}"
resultLabel["text"] = "1๋ถํฐ 100์ฌ์ด์ ์ซ์๋ฅผ ์
๋ ฅํ์์ค."
tryButton.config(state=NORMAL)
window = Tk()
window.configure(bg="white")
window.title("์ซ์๋ฅผ ๋ง์ถฐ๋ณด์ธ์!")
window.geometry("500x120")
titleLabel = Label(window, text="์ซ์ ๊ฒ์์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค!", bg="white")
titleLabel.pack()
guessField = Entry(window)
guessField.pack(side="left")
tryButton = Button(window, text="์๋", fg="green", bg="white", command=guessing)
tryButton.pack(side="left")
resetButton = Button(window, text="์ด๊ธฐํ", fg="red", bg="white", command=reset)
resetButton.pack(side="left")
triesLabel = Label(window, text=f"์๋ ํ์: {tries}/{max_tries}", bg="white")
triesLabel.pack(side="left")
resultLabel = Label(window, text="1๋ถํฐ 100์ฌ์ด์ ์ซ์๋ฅผ ์
๋ ฅํ์์ค.", bg="white")
resultLabel.pack(side="left")
window.mainloop()