๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Python Programming/์–ด์„œ์™€ ํŒŒ์ด์ฌ์€ ์ฒ˜์Œ์ด์ง€!_2019 Ver

[์–ด์„œ์™€ ํŒŒ์ด์ฌ์€ ์ฒ˜์Œ์ด์ง€!_2019 Ver] 9์žฅ Lab ๋„์ „๋ฌธ์ œ ํ’€์ด

by A Lim Han 2023. 2. 22.

# 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()