from tkinter import *

gg = Tk()
gg.title("Сейф")
gg.geometry("600x400")
gg.resizable(False, False)

SECRET_CODE = "1234"

current_code = ""

label_screen = None
label_status = None
btn_clear = None

def startGame():
    global label_screen, label_status, btn_clear
    btnstart.pack_forget()
    label_screen = Label(
        gg,
        text="",
        font=("Digital", 24, "bold"),
        bg="black",
        fg="lime",
        width=8,
        height=1,
        relief="sunken",
        anchor="e"
    )
    label_screen.pack(pady=20)
    Label(gg, text="Введите код:", font=("Arial", 12)).pack()
    label_status = Label(gg, text="", font=("Arial", 12), fg="red")
    label_status.pack(pady=5)

    btn_clear = Button(
        gg,
        text="C",
        font=("Arial", 12, "bold"),
        bg="orange",
        fg="white",
        width=4,
        command=clear_code
    )
    btn_clear.pack(pady=5)

    frame_buttons = Frame(gg)
    frame_buttons.pack(pady=10)

    btn1 = Button(
        frame_buttons,
        text="1",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(1)
    )
    btn1.grid(row=0, column=0, padx=2, pady=2)

    btn2 = Button(
        frame_buttons,
        text="2",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(2)
    )
    btn2.grid(row=0, column=1, padx=2, pady=2)

    btn3 = Button(
        frame_buttons,
        text="3",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(3)
    )
    btn3.grid(row=0, column=2, padx=2, pady=2)

    btn4 = Button(
        frame_buttons,
        text="4",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(4)
    )
    btn4.grid(row=1, column=0, padx=2, pady=2)

    btn5 = Button(
        frame_buttons,
        text="5",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(5)
    )
    btn5.grid(row=1, column=1, padx=2, pady=2)

    btn6 = Button(
        frame_buttons,
        text="6",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(6)
    )
    btn6.grid(row=1, column=2, padx=2, pady=2)

    btn7 = Button(
        frame_buttons,
        text="7",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(7)
    )
    btn7.grid(row=2, column=0, padx=2, pady=2)

    btn8 = Button(
        frame_buttons,
        text="8",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(8)
    )
    btn8.grid(row=2, column=1, padx=2, pady=2)

    btn9 = Button(
        frame_buttons,
        text="9",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(9)
    )
    btn9.grid(row=2, column=2, padx=2, pady=2)

    btn0 = Button(
        frame_buttons,
        text="0",
        font=("Arial", 14),
        width=4,
        height=2,
        bg="#E0E0E0",
        command=lambda: on_button_click(0)
    )
    btn0.grid(row=3, column=1, padx=2, pady=2)

def on_button_click(num):
    global current_code

    if len(current_code) >= 4:
        return

    current_code += str(num)
    update_screen()

    if len(current_code) == 4:
        check_code()

def clear_code():
    global current_code
    current_code = ""
    update_screen()
    if label_status:
        label_status.config(text="")

def update_screen():
    if label_screen:
        label_screen.config(text=current_code)

def check_code():
    global current_code
    if current_code == SECRET_CODE:
        label_status.config(text="✅ Сейф открыт!", fg="green")
    else:
        label_status.config(text="❌ Неверный код!", fg="red")
        gg.after(1500, clear_code)

btnstart = Button(
    gg,
    text="Начать игру",
    font=("Arial", 14),
    bg="#4CAF50",
    fg="white",
    padx=20,
    pady=10,
    command=startGame
)
btnstart.pack(pady=50)

gg.mainloop()