我如何将输入文本框的结果分配给变量,然后传递给后端的函数?

时间:2018-11-10 21:57:31

标签: python oop tkinter widget tkinter-entry

我在使用tkinter编程子手游戏时遇到一些问题,前端和后端分开,并且都使用OOP(我知道在后端使用OOP并不是真正必要的,可能已经作为图书馆更好,但我正在将其作为学习练习)。我是tkinter的新手,我想知道如何将输入文本框的结果分配给变量,然后传递给后端的函数。我还想知道我的self.letter_row和self.guesses_left标签是否会在后端更新时更新,尽管我在game_gui()启动时将它们分配到了前端,但我不确定它们是否会自动执行此操作。这是所有的前端代码...

from tkinter import *
from tkinter.ttk import *
import hangmanSkeleton as hm

class game_gui():

    def __init__(self,master):
        self.master = master
        master.title("Hangman")

        this_game = hm.Hangman()

        self.word = this_game.word
        self.letter_row = this_game.letter_row
        self.letters_guessed = this_game.letters_guessed
        self.guesses_left = this_game.guesses_left
        self.letter = StringVar()

        self.lbl_word = Label(master, text = "Welcome to Hangman!")
        self.lbl_word.grid(row = 0,padx = 10 , pady = 15, columnspan = 2)

        self.lbl_row = Label(master, text = self.letter_row)
        self.lbl_row.grid(row = 1, column = 0, padx = 15, pady = 10)

        self.lbl_guesses = Label(master, text = "Guesses Left: " + str(self.guesses_left))
        self.lbl_guesses.grid(row = 1, column = 1, padx = 15, pady = 10)

        self.entry_letter= Entry(master, textvariable = self.letter)
        self.entry_letter.grid(row = 2, padx = 10, pady = 20, columnspan = 2)

        self.guess_button = Button(master, text = "Guess Letter", command=lambda: this_game.guess_letter(self.letter))
        self.guess_button.grid(row = 3, padx = 10, pady = 10, columnspan = 2) 


root = Tk()
gui = game_gui(root)
root.mainloop()

这只是我试图在self.guess_button()中使用的后端功能。

def guess_letter(self, letter): 
    try: 
        if type(letter) != str or len(letter) != 1 or letter not in ascii_lowercase:
            raise TypeError
        if letter in self.letters_guessed:
            raise ValueError
        elif letter in self.word:
            for count in range (len(self.word)):
                if letter==self.word[count]:
                    self.letter_row = self.letter_row[0:count] + letter + self.letter_row[count+1:]
        else:
            pass
        self.guesses_left = self.guesses_left-1 #take one away from guesses
    except TypeError:
        print("Value given is not a letter")
    except ValueError:
        print("Letter has already been guessed")

谢谢!

1 个答案:

答案 0 :(得分:0)

  

评论:我仍然无法通过我的标签self.letter_row和self.guesses_left进行更新

是的,这些变量未绑定到Label,我建议改为使用Label.configure(text=...
注意:我只显示.this_game.letter_row

添加功能update_labels(....configure(...标签文本:

def update_labels(self):
    self.lbl_row.configure(text=self.this_game.letter_row)
    # and so on

.__init__(... .guess_letter(...中的最后处调用此功能。
完全不需要变量self.letter_row


  

问题:如何将输入文本框的结果分配给变量,然后传递给后端的函数。

更改以下内容:

  • 使变量this_game成为class game_gui的成员

    self.this_game = hm.Hangman()
    
  • guess_letter(...中添加辅助方法class game_gui
    将字母传递给.this_game.guess_letter(...
    清除.letter中的字母。

    def guess_letter(self, letter):
        self.this_game.guess_letter(letter.get())
        letter.set('')
    
  • 更改command,指向助手方法:

    ..., command=lambda: self.guess_letter(self.letter)