【发布时间】:2020-05-15 21:14:03
【问题描述】:
当我启动程序时,似乎没有任何问题。只有当我关闭程序时才会收到错误消息:
_tkinter.TclError: 无效的命令名称“.!canvas”
当我运行我的程序时,我的按钮也没有任何影响。我评论了给我错误的代码行。抱歉,代码太长了,但错误出现在 draw_letters() 函数中。我不知道这个错误可能是什么。
编辑:将 mainwindow.mainloop() 放在程序末尾没有帮助,它只会给我一个不同的错误。这是因为我的程序需要在更新画布之前等待用户输入。
import random
import tkinter as tk
from tkinter import font
# I'm using this import to create a command with a parameter for my buttons
from functools import partial
class EventListener(object):
def __init__(self, current_letter):
self.current_letter = current_letter
def key_input(self, char):
self.current_letter = char
def get_current_letter(self):
return self.current_letter
def get_random_word():
word_list = []
with open('words.txt', 'r') as word_file:
for word in word_file:
if 4 < len(word) < 14:
word_list.append(word.strip('\n'))
return word_list[random.randint(0, len(word_list) - 1)]
def draw_gallows():
canvas.create_line(60, 350, 300, 350, width=2)
canvas.create_line(105, 350, 105, 20, width=2)
canvas.create_line(105, 20, 240, 20, width=2)
canvas.create_line(240, 20, 240, 50, width=2)
canvas.create_line(105, 85, 170, 20, width=2)
canvas.create_line(105, 95, 180, 20, width=2)
def draw_body_part(mistakes):
if mistakes <= 1:
canvas.create_oval(209, 50, 269, 110, width=2)
elif mistakes <= 2:
canvas.create_line(240, 110, 240, 260, width=2)
elif mistakes <= 3:
canvas.create_line(240, 180, 190, 120, width=2)
elif mistakes <= 4:
canvas.create_line(240, 180, 290, 120, width=2)
elif mistakes <= 5:
canvas.create_line(240, 260, 200, 330, width=2)
elif mistakes <= 6:
canvas.create_line(240, 260, 280, 330, width=2)
def draw_dashes(word, x):
for i in word:
canvas.create_line(x, 260, x + 25, 260)
x += 36
def draw_letters(letters, x):
for letter in letters:
canvas.create_text(x, 260, letter) # this is the line that is erring #####################
x += 36
# TODO: Set up end condition window
def end_condition(hasWon):
pass
def play_one_round():
word = get_random_word()
print(word)
correct_guesses = []
mistakes = 0
x = (700 // len(word)) + 260
draw_dashes(word, x)
main_window.mainloop()
while (len(correct_guesses) != len(word)) or (mistakes == 6):
guess = button_clicked.current_letter
print(guess)
if guess in word:
correct_guesses.append(guess)
draw_letters(correct_guesses, x)
elif guess not in word:
mistakes += 1
draw_body_part(mistakes)
if mistakes == 6:
end_condition(hasWon=False)
else:
end_condition(hasWon=True)
# creating the window, canvas and buttons
main_window = tk.Tk()
main_window.title('Hangman')
main_window.geometry('800x580+240+30')
main_window.resizable(False, False)
canvas = tk.Canvas(main_window, background='white', width=800, height=360)
canvas.grid(row=0, column=0)
keyboard_panel = tk.Frame(main_window)
keyboard_panel.grid(row=1, column=0)
helv17 = font.Font(family='Helvetica', size=17, weight='bold')
helv12 = font.Font(family='Helvetica', size=12)
button_clicked = EventListener('')
for i in range(ord('a'), ord('z') + 1):
if i < ord('j'):
column = ord('a')
row = 0
elif i < ord('s'):
column = ord('j')
row = 1
else:
column = ord('s')
row = 2
# the partial keyword allows you to have a command with a parameter
button = tk.Button(keyboard_panel, text=chr(i), font=helv17,
command=partial(button_clicked.key_input, chr(i)), width=4)
button.grid(row=row, column=i - column, padx=10, pady=10)
new_game_button = tk.Button(keyboard_panel, text='New Game', font=helv12)
new_game_button.grid(row=2, column=8)
draw_gallows()
play_one_round()
【问题讨论】:
标签: python python-3.x tkinter tkinter-canvas