Mam problem z mają aplikacją (tkinter).

0

Dzień dobry.

Program Wisielec.
Chciałbym zrobić żeby po zgadnięciu całego hasła było wyświetlone okno messagebox i jeśli się kliknie na literę to nie da się na nią kliknąć drugi raz.
Mam także pytanie jaką komendą można wyłączyć całe okno/program.

#inport
from tkinter import messagebox
from tkinter import *
import random
import linecache
#okno
root = Tk()
root.resizable(0,0)
root.title("Wisielec")
root.geometry("800x600")
font = ("Ariel" ,20,"bold")
#randomowy tex
randomLiczba = random.randint(1,20)
wiersz = linecache.getline('Words.txt', randomLiczba)
wiersz = wiersz.rstrip()
password = wiersz #mx 9 znakuw
password = password.upper()
#alfabet
alphabet = "AĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŻŹ"
#wspułrzendne
letter1X = 10
letter1Y = 260
letter2X = 10
#zmiene globalne
errors = 0
passLabels = [] #pusta tablica do kratek
#logika
def wrongLetter():
    globals()["errors"] += 1
    if errors == 1:
        ploton.create_line(20, 560 ,95 , 520, width = 10, fill = "black")

    if errors == 2:
        ploton.create_line(170, 560 ,95 , 520, width = 10, fill = "black")

    if errors == 3:
        ploton.create_line(95, 520 ,95 , 20, width = 10, fill = "black")

    if errors == 4:
        ploton.create_line(95, 20 ,245 , 20, width = 10, fill = "black")

    if errors == 5:
        ploton.create_line(245, 20 ,245 , 70, width = 10, fill = "black")

    if errors == 6:
        ploton.create_line(95, 150 ,170 , 20, width = 10, fill = "black")

    if errors == 7:
        ploton.create_oval(220, 70 ,270 , 120, width = 5)#glowa

    if errors == 8:
        ploton.create_line(245, 120 ,245 , 230, width = 5, fill = "black")

    if errors == 9:
        ploton.create_line(245, 150 ,205 , 190, width = 5, fill = "black")

    if errors == 10:
        ploton.create_line(245, 150 ,285 , 190, width = 5, fill = "black")

    if errors == 11:
        ploton.create_line(245, 230 ,205 , 270, width = 5, fill = "black")

    if errors == 12:
        ploton.create_line(245, 230 ,285 , 270, width = 5, fill = "black")

    if errors == 12:
        messagebox.showerror("GAME OVER", "GAME OVER: GRA SKOŃCZONA")

def checkLetter(event):
    litera = event.widget["text"]
    
    if litera in password:
        index = 0
        for letter in password:
            if letter == litera:
                event.widget["fg"] = "white"
                event.widget["bg"] = "green"
                litera = event.widget["text"]
                etykieta = passLabels[index]
                etykieta["text"] = litera
            index += 1
    else:
        wrongLetter()
        event.widget["fg"] = "red"
        event.widget["bg"] = "black"

#interfejs

for letter in alphabet:
    lbl = Label(root, text = letter, font = font, fg = "black", )
    lbl.place(x = letter1X, y = letter1Y, width = 35, height = 35)
    lbl.bind("<Button-1>", checkLetter)
    letter1X += 40
    if (letter1X > 405):
        letter1Y += 80
        letter1X = 10

for letter in password:
        lbl = Label(root, text = "", font = font, bg = "white", bd = 2, relief = "solid")
        lbl.place(x = letter2X, y = 50, width = 40, height = 40)
        letter2X += 45    
        passLabels.append(lbl)

ploton = Canvas(root, bg = "gold")
ploton.place(x = 450,y = 10, width = 340, height = 580)


root.mainloop()

1

Po pierwsze: ortografia. :)

Kacperek7 napisał(a):

Chciałbym zrobić żeby po zgadnięciu całego hasła było wyświetlone okno messagebox

No to podobnie jak to:

    if errors == 12:
        messagebox.showerror("GAME OVER", "GAME OVER: GRA SKOŃCZONA")

trzeba zrobić mniej więcej w tym samym miejscu:

    if password == to_co_zgadl_uzytkownik: # a to musisz jakoś budować w trakcie wciskania klawiszy przez użytkownika
        messagebox.showinfo("WYGRAŁES!", "GRATULACJE, WYGRAŁEŚ!")

i jeśli się kliknie na literę to nie da się na nią kliknąć drugi raz.

Lepiej chyba zrobić literki jako Button (z wyglądem FLAT, będą wyglądać jak etykiety) i zablokować je po kliknięciu ustawiając state='disabled'.

Mam także pytanie jaką komendą można wyłączyć całe okno/program.

Metoda destroy dla okienka powinna zadziałać.

1
Kacperek7 napisał(a):

Dzięki! A do destroy jak go mogę użyć?

root.destroy() powinno dać radę...

0

To jest poprawiony program.
Wywala błąd którego nie rozumiem.
Pytanie jak schować konsole?

#inport
from tkinter import messagebox
from tkinter import *
import random
import linecache
#okno
root = Tk()
root.resizable(0,0)
root.title("Wisielec")
root.geometry("800x600")
font = ("Ariel" ,20,"bold")
#alfabet
alphabet = "AĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŻŹ"
#wspułrzendne
letter1X = 10
letter1Y = 260
letter2X = 10
#zmiene globalne
decyzja = ""
password = ""
errors = 0
passLabels = [] #pusta tablica do kratek
#logika
def gameOver():
    over = "¡GAME OVER: GRA SKOŃCZONA!" "                                           HASŁO TO:   "   +password+ "                                                                   ¿CHCESZ DALEJ GRAĆ?"
    if errors >= 12:
        decyzja = messagebox.askyesno("GAME OVER",over)

        if decyzja == True:
            globals()["errors"] = 0
            randomText()
        else:
            root.destroy()
   


def randomText():
    globals()["password"]
    randomLiczba = random.randint(1,100)
    wiersz = linecache.getline('password.txt', randomLiczba)
    wiersz = wiersz.rstrip()
    password = wiersz #mx 9 znakuw
    password = password.upper()
    return password

def wrongLetter():
    globals()["errors"] += 1
    if errors == 1:
        ploton.create_line(20, 560 ,95 , 520, width = 10, fill = "black")

    if errors == 2:
        ploton.create_line(170, 560 ,95 , 520, width = 10, fill = "black")

    if errors == 3:
        ploton.create_line(95, 520 ,95 , 20, width = 10, fill = "black")

    if errors == 4:
        ploton.create_line(95, 20 ,245 , 20, width = 10, fill = "black")

    if errors == 5:
        ploton.create_line(245, 20 ,245 , 70, width = 10, fill = "black")

    if errors == 6:
        ploton.create_line(95, 150 ,170 , 20, width = 10, fill = "black")

    if errors == 7:
        ploton.create_oval(220, 70 ,270 , 120, width = 5)#glowa

    if errors == 8:
        ploton.create_line(245, 120 ,245 , 230, width = 5, fill = "black")

    if errors == 9:
        ploton.create_line(245, 150 ,205 , 190, width = 5, fill = "black")

    if errors == 10:
        ploton.create_line(245, 150 ,285 , 190, width = 5, fill = "black")

    if errors == 11:
        ploton.create_line(245, 230 ,205 , 270, width = 5, fill = "black")

    if errors == 12:
        ploton.create_line(245, 230 ,285 , 270, width = 5, fill = "black")

    gameOver()

def checkLetter(event):
    litera = event.widget["text"]
    if litera in password:
        index = 0
        for letter in password:
            if letter == litera:
                event.widget["fg"] = "white"
                event.widget["bg"] = "green"
                litera = event.widget["text"]
                etykieta = passLabels[index]
                etykieta["text"] = litera
            index += 1
    else:
        wrongLetter()
        event.widget["fg"] = "red"
        event.widget["bg"] = "black"

#interfejs

for letter in alphabet:
    lbl = Label(root, text = letter, font = font, fg = "black", )
    lbl.place(x = letter1X, y = letter1Y, width = 35, height = 35)
    lbl.bind("<Button-1>", checkLetter)
    letter1X += 40
    if (letter1X > 405):
        letter1Y += 80
        letter1X = 10
        
password = randomText()
for letter in password:
        lbl = Label(root, text = "", font = font, bg = "white", bd = 2, relief = "solid")
        lbl.place(x = letter2X, y = 50, width = 40, height = 40)
        letter2X += 45    
        passLabels.append(lbl)

ploton = Canvas(root, bg = "gold")
ploton.place(x = 450,y = 10, width = 340, height = 580)


root.mainloop()

1 użytkowników online, w tym zalogowanych: 0, gości: 1