Можете пожалуйста исправить этот код так, чтобы при нажатии на букву она становилась красной или зелёной, взависимости от того правильная она или нет
Код:
import random
import tkinter as tk
from tkinter import *
logs = tk.Tk()
logs.title("Hangman")
logs.geometry("900x800")
word_list = ['banans', 'cisiņa']
guessed_word = ""
error_count = 0
burti=[]
x="AĀBCČDEĒFGĢHIĪJKĶLĻMNŅOPRSŠTUŪVZŽ"
for i in range(33):
burti.append(Button(logs, text = x[i], fg = "white", bg="blue", width=3, font="Arial 20 bold"))
burti[i].place(x=550+70*(i%5), y=100+70*(i//5))
burti[i].bind(' ', lambda event, letter=x[i]: check_letter(letter))
hangman_parts = ['head', 'body', 'left arm', 'right arm', 'left leg', 'right leg', 'stick_bg', 'stick_sm', 'rope', 'rope_oval']
def prepare_new_word():
global guessed_word
guessed_word = random.choice(word_list)
word_label.config(text=' '.join(['_' for i in range(len(guessed_word))]))
global error_count
error_count = 0
canvas.delete("all")
def check_letter(letter):
global error_count
new_guess = ''
old_guess = word_label['text']
found = False
for i in range(len(guessed_word)):
if letter.lower() == guessed_word[i] and old_guess[i] == '_':
new_guess += letter.lower()
found = True
else:
new_guess += old_guess[i]
if not found:
error_count += 1
if error_count == 10:
print("Ты проиграл!")
else:
draw_hangman()
else:
word_label.config(text=new_guess)
if new_guess == guessed_word:
print("Ты победил!")
def draw_hangman():
if error_count >= len(hangman_parts):
return
part = hangman_parts[error_count]
if part == 'head':
canvas.create_oval(50, 50, 100, 100)
elif part == 'body':
canvas.create_line(75, 100, 75, 200)
elif part == 'left arm':
canvas.create_line(75, 125, 50, 150)
elif part == 'right arm':
canvas.create_line(75, 125, 100, 150)
elif part == 'left leg':
canvas.create_line(75, 200, 50, 225)
elif part == 'right leg':
canvas.create_line(75, 200, 100, 225)
elif part == 'stick_bg':
canvas.create_line(150, 225, 150, 20)
elif part == 'stick_sm':
canvas.create_line(150, 20, 100, 20)
elif part == 'rope':
canvas.create_oval(45, 125, 55, 135)
elif part == 'rope_oval':
canvas.create_oval(95, 125, 105, 135)
canvas = tk.Canvas(logs, width=200, height=250)
canvas.pack()
word_label = tk.Label(logs, text='')
word_label.pack()
prepare_new_word()
new_game_button = tk.Button(logs, text='Новая игра', command=prepare_new_word)
new_game_button.pack()
logs.mainloop()
Помогите пожалуйста дополнить код. Заранее благодарен за помощь!
Ответы
Відповідь:import random
import tkinter as tk
from tkinter import *
logs = tk.Tk()
logs.title("Hangman")
logs.geometry("900x800")
word_list = ['banans', 'cisiņa']
guessed_word = ""
error_count = 0
burti=[]
x="AĀBCČDEĒFGĢHIĪJKĶLĻMNŅOPRSŠTUŪVZŽ"
for i in range(33):
burti.append(Button(logs, text = x[i], fg = "white", bg="blue", width=3, font="Arial 20 bold"))
burti[i].place(x=550+70*(i%5), y=100+70*(i//5))
burti[i].bind(' ', lambda event, letter=x[i]: check_letter(letter))
hangman_parts = ['head', 'body', 'left arm', 'right arm', 'left leg', 'right leg', 'stick_bg', 'stick_sm', 'rope', 'rope_oval']
def prepare_new_word():
global guessed_word
guessed_word = random.choice(word_list)
word_label.config(text=' '.join(['_' for i in range(len(guessed_word))]))
global error_count
error_count = 0
canvas.delete("all")
def check_letter(letter):
global error_count
new_guess = ''
old_guess = word_label['text']
found = False
for i in range(len(guessed_word)):
if letter.lower() == guessed_word[i] and old_guess[i] == '_':
new_guess += letter.lower()
found = True
else:
new_guess += old_guess[i]
if not found:
error_count += 1
if error_count == 10:
print("Ты проиграл!")
else:
draw_hangman()
else:
word_label.config(text=new_guess)
if new_guess == guessed_word:
print("Ты победил!")
def draw_hangman():
if error_count >= len(hangman_parts):
return
part = hangman_parts[error_count]
if part == 'head':
canvas.create_oval(50, 50, 100, 100)
elif part == 'body':
canvas.create_line(75, 100, 75, 200)
elif part == 'left arm':
canvas.create_line(75, 125, 50, 150)
elif part == 'right arm':
canvas.create_line(75, 125, 100, 150)
elif part == 'left leg':
canvas.create_line(75, 200, 50, 225)
elif part == 'right leg':
canvas.create_line(75, 200, 100, 225)
elif part == 'stick_bg':
canvas.create_line(150, 225, 150, 20)
elif part == 'stick_sm':
canvas.create_line(150, 20, 110, 20)
elif part == 'rope':
canvas.create_oval(45, 125, 55, 135)
elif part == 'rope_oval':
canvas.create_oval(95, 125, 105, 135)
canvas = tk.Canvas(logs, width=200, height=250)
canvas.pack()
word_label = tk.Label(logs, text='')
word_label.pack()
prepare_new_word()
new_game_button = tk.Button(logs, text='Новая игра', command=prepare_new_word)
new_game_button.pack()
logs.mainloop()
Помогите пожалуйста дополнить код. Заранее благодарен за помощь!
Пояснення: вроде я все ошыбки ашол у тебя их было 5 попробуй если нет я ошыбся сорян