Tkinter - 为什么我的按钮执行命令?

时间:2015-04-27 23:17:40

标签: python python-3.x tkinter

我确定我遗漏了一些可笑的东西,但我已经盯着它看了20分钟而且我不知道它是什么。

class GUI:
  def __init__(self):
  # creating frame and window
  self.winMain = tk.Tk()
  self.topFrame = tk.Frame(self.winMain)
  self.midFrame = tk.Frame(self.winMain)
  self.botFrame = tk.Frame(self.winMain)
  # creating labels
  self.wordLabel = tk.Label(self.topFrame,text="Word to unscramble",width=18)
  self.wordLabel.pack(side="left")
  # scrambled word
  self.scrambledWord = tk.StringVar()
  # open file of scrambled words
  dictList = open('dict.txt', 'r')
  words = [] # empty list to fill with words
  for line in dictList:
         words.append(line)
  word = random.choice(words)
  print(word)
  word = ''.join(random.sample(word, len(word)))

  self.scrambledWord.set(word)
  # label of scrambled word
  self.ScWd = tk.Label(self.topFrame, textvariable=self.scrambledWord, width=len(word))
  self.ScWd.pack(side="right")

  # entry label
  self.guessIn = tk.Label(self.midFrame,text="Your guess:", width=15)
  self.guessIn.pack(side="left")

  # input box
  self.guess = tk.Entry(self.midFrame, width=15)
  self.guess.pack(side="right")

  # guess button
  self.guessButton = tk.Button(self.botFrame,text="Guess",command=self.guess)
  self.guessButton.pack(side="left")

  # status variable
  self.stat = tk.StringVar()
  # status label
  self.status = tk.Label(self.botFrame, textvariable=self.stat,width=10)
  self.status.pack(side="right")



  self.topFrame.pack(side="top")
  self.midFrame.pack()
  self.botFrame.pack()
  tk.mainloop()
 def guess(self):
  correct = False
  userGuess = self.guess.get()
  self.stat.set(userGuess)
  if userGuess == self.scrambledWord:
    self.stat.set("correct")
    correct = True
    print(correct)

很抱歉发布了整件事,但我不知道问题出在哪里。非常感谢任何帮助。

0 个答案:

没有答案