我是编程的初学者,我正在用tkinter编写一个简单的hangman游戏,不知道如何解决错误。
Traceback (most recent call last):
File "/Users/ana/Documents/primer7.py", line 62, in <module>
class MyGUI():
File "/Users/ana/Documents/primer7.py", line 93, in MyGUI
tkinter.mainloop()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 405, in mainloop
_default_root.tk.mainloop(n)
AttributeError: 'NoneType' object has no attribute 'tk'
你可以帮我解决这个问题吗?
整体代码应该没问题,有没有人有任何建议如何让它更简单/更简单?或者如何制作更好的GUI(按钮),我不太了解它?
import tkinter
import tkinter.messagebox
import random
#fruit category
easyWords = ['apple', 'orange', 'mango', 'peach', 'guava']
#space category
mediumWords = ['atmosphere', 'jupiter', 'quasar', 'satellite', 'asteroid']
#science category
hardWords = ['biochemical', 'hemoglobin', 'emulsify', 'reactant', 'dynamo']
def setting():
wordChoice = ''
difficulty = input('''Welcome to hangman, select your difficulty.
Type, easy, medium, or hard to begin:''')
if difficulty == 'easy':
wordChoice = easyWords.random
print('You have selected easy mode, start guessing your letters now in the game window. The category is: fruit')
if difficulty == 'medium':
wordChoice = mediumWords.random
print('You have selected medium mode, start guessing your letters now in the game window. The category is: space')
if difficulty == 'hard':
wordChoice = hardWords.random
print('You have selected hard mode, start guessing your letters now in the game window. The category is: science')
def game():
missGuess = 0
guesses = ''
for char in wordChoice:
label3.print(char),
if char in guesses:
print(char),
else:
label3.print("_"),
missGuess += 1
if missGuess == 1:
label1.print('head')
if missGuess == 2:
label1.print('torso')
if missGuess == 3:
label1.print('left arm')
if missGuess == 4:
label1.print('right arm')
if missGuess == 5:
label1.print('left leg')
if missGuess == 6:
label1.print('right leg'),
class MyGUI():
def __init__(self):
self.main_window = tkinter.tk()
#create needed frames
self.top_frame = tkinter.Frame(self.main_window)
self.center_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)
#create top frame labels
self.label1 = tkinter.Label(self.top_frame, text='Hangman parts:')
self.label2 = tkinter.Label(self.top_frame, text=' ')
#center frame labels
self.label3 = tkinter.Label(self.center_frame, text=' ')
#bottom frame labels
self.label4 = tkinter.Label(self.bottom_frame, text='Guess a letter:')
self.entry1 = tkinter.Entry(self.bottom_frame, width=5)
self.button1 = tkinter.Button(self.bottom_frame, text='Guess', command=self.game) #calls the game method
self.button2 = tkinter.Button(self.bottom_frame, text='Exit', command=self.main_window.destroy)
#pack top frame labels
self.label1.pack(side='left')
self.label2.pack(side='right')
#pack center frame
self.label3.pack(side='top')
#bottom frame
self.label4.pack(side='left')
self.entry1.pack(side='left')
self.button1.pack(side='left')
self.button2.pack(side='left')
tkinter.mainloop()
setting()
main()
答案 0 :(得分:0)
dataset = open("data1.5.txt", "r")
array = dataset.read().split(", \n")
def float_report_errors(val):
try:
return float(val)
except ValueError as e:
print("float(", repr(val), ") -->", repr(e))
array2 = [float_report_errors(i) for i in array]
print("Indices with errors:", *(i for i,v in enumerate(array2) if v is None))
实例上调用 mainloop()
。改变这一行:
tkinter.Tk()
为:
tkinter.mainloop()
另外,在self.main_window.mainloop()
类的__init__
方法下正确缩进该行。
所以MyGUI
变为:
MyGUI
我希望它有所帮助。
作为旁注,代码中的任何位置都不会调用class MyGUI():
def __init__(self):
self.main_window = tkinter.tk()
#create needed frames
self.top_frame = tkinter.Frame(self.main_window)
self.center_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)
#create top frame labels
self.label1 = tkinter.Label(self.top_frame, text='Hangman parts:')
self.label2 = tkinter.Label(self.top_frame, text=' ')
#center frame labels
self.label3 = tkinter.Label(self.center_frame, text=' ')
#bottom frame labels
self.label4 = tkinter.Label(self.bottom_frame, text='Guess a letter:')
self.entry1 = tkinter.Entry(self.bottom_frame, width=5)
self.button1 = tkinter.Button(self.bottom_frame, text='Guess', command=self.game) #calls the game method
self.button2 = tkinter.Button(self.bottom_frame, text='Exit', command=self.main_window.destroy)
#pack top frame labels
self.label1.pack(side='left')
self.label2.pack(side='right')
#pack center frame
self.label3.pack(side='top')
#bottom frame
self.label4.pack(side='left')
self.entry1.pack(side='left')
self.button1.pack(side='left')
self.button2.pack(side='left')
self.main_window.mainloop()
,并且提交的代码中也无法使用MyGUI
函数。我不知道你游戏的逻辑,所以我所做的只是解决了错误信息,剩下的由你决定。