弹出错误信息说"请输入1个字母"

时间:2016-01-11 02:00:57

标签: tkinter python-3.5 tkinter-canvas

from tkinter import *
from Hangman import *

#GUI for hangman game
class HangmanGui:
    def __init__(self):
        self.game = HangmanGame()
        self.root = Tk()
        self.root.title('Hangman')
        makeHangman = self.makeHangman('empty.gif')
        makeHangman.pack() 
        frame2= self.makeframe2()
        frame2.pack() 
        self.root.mainloop()

    def makeHangman(self,filename):
        frame1= Frame(self.root)
        self.img = PhotoImage(file = filename)
        ht = self.img.height()
        wd = self.img.width()
        canvas = Canvas(self.root,
                        height = ht,
                        width = wd
                        )      
        canvas.create_image(0, 0,
                            image = self.img,
                            anchor = NW
                            )
        canvas.pack()
        return frame1

    def makeframe2(self):    
        frame2= Frame(self.root) 
        self.wrong_letter= Label(frame2,
                                 text=self.game.getWrongList)
        self.winner= Label(frame2,
                           text=self.game.win)
        self.correct_answer= Label(frame2,
                                   text=self.showcorrect)
        self.user_input= Entry(frame2, width=10)
        self.button= Button(frame2,
                            text='Guess', command=self.buttonEffect)

        self.wrong_letter.pack()
        self.winner.pack()
        self.correct_answer.pack()
        self.user_input.pack()
        self.button.pack()

        return frame2

    def buttonEffect(self):
        guess=self.user_input.get
        guess1=str(guess)
        if(len(guess1)!=1):
            messagebox.showerror('error','Please only enter 1 letter.')
        #elif(guess1.isalpha()==False):
            #messagebox.showerror('Error', 'Please enter a letter.')

    def showcorrect(self,guess1):
        display = "_" * len(guess1)
        if self.game.chooseword.find(guess1)>=1:
            print()

        wronglist=self.game.getWrongList()
        self.label.config(text=str(wronglist))

        if len(wronglist)== 1:
            makeHangman = self.makeHangman('hangman head.gif')
        elif len(wrongList)== 2:
            makeHangman = self.makeHangman('hangman body.gif')
        elif len(wrongList)== 3:
            makeHangman = self.makeHangman('hangman 1 arm.png')
        elif len(wrongList)== 4:
            makeHangman = self.makeHangman('hangman 2 arms.jpg')
        elif len(wrongList)== 5:
            makeHangman = self.makeHangman('hangman 1 leg.png')
        elif len(wrongList)== 6:
            makeHangman = self.makeHangman('hangman.png')


HangmanGui()

嗨,我正在尝试使用python Tkinter gui制作一个刽子手游戏,但我在用户输入一个字母的部分遇到麻烦,每当我输入一个字母时,我会收到一个错误消息框弹出说&#34 ;请输入1个字母"即使我只输入一个字母,检查用户是否输入1个字母的代码部分位于" Def ButtonEffect"功能。我想知道是否有人可以帮我解释为什么即使我输入1个字母我的消息框也会弹出。

1 个答案:

答案 0 :(得分:0)

问题在于:

guess=self.user_input.get

您没有调用函数get,只需将函数分配给变量即可。稍后您执行str(guess)会产生一个长字符串。

要解决此特定问题,请添加括号以将引用转换为函数调用:

guess=self.user_input.get()