2016-05-22 133 views
0

我一直在用Python书帮助学习tkinter。然而,python书是为Python 2编写的,而我正在使用python 3.这已经产生了一些错误,到现在为止我已经能够解决它们,并借助其他问题在这里。这是一个基本的程序,显示一个带按钮的窗口玩3场比赛或摧毁窗户。当前两场比赛(摇滚纸剪刀和hang子手)中的任何一场比赛都打开时,他们应该打开一个Toplevel(),其中有各种按钮可供您玩游戏。我还没有为第三款游戏编写图形代码。他们不玩,因为有一个错误:_tkinter.TclError:未知选项“-padding” 我没有找到关于在线的任何细节。我知道未知选项意味着该属性/关键字不存在于我的tkinter和python 3版本中。 错误行如下所示: rpsFrame = Frame(rpsWindow,padding ='3 3 12 12',width = 300)Tkinter的Python 3错误:_tkinter.TclError:未知选项“-padding”

如果您需要它,错误和代码如下。 (此代码不会没有额外的代码运行) (如果你想这个代码,我会添加它,但我想保持这种稍短)

以下是完整的错误回溯:

Exception in Tkinter callback 

Traceback (most recent call last): 
    File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__ 
    return self.func(*args) 
    File "E:\Python\Python Book\Graphics\Hangman.py", line 149, in gui 
    hmFrame = Frame(hmWindow, padding = "3 3 12 12", width = 300) 
    File "C:\Python34\lib\tkinter\__init__.py", line 2582, in __init__ 
    Widget.__init__(self, master, 'frame', cnf, {}, extra) 
    File "C:\Python34\lib\tkinter\__init__.py", line 2122, in __init__ 
    (widgetName, self._w) + extra + self._options(cnf)) 
_tkinter.TclError: unknown option "-padding" 

下面是接口代码:

from tkinter import * 
import RockPaperScissors 
import Hangman 
import PokerDice 

root = Tk() 
root.title("Linux User & Developer's Mega Games Collection") 

mainframe = Frame(root, height = 200, width = 500) 
mainframe.pack_propagate(0) 
mainframe.pack(padx = 5, pady = 5) 

intro = Label(mainframe, text = """Welcome to Linux User & Developers Mega 
Microgames Collection. 
Select one of the following games to play: 
""") 
intro.pack(side = TOP) 

rpsButton = Button(mainframe, text = "Rock, Paper, Scissors", 
        command = RockPaperScissors.gui) 
rpsButton.pack() 

hmButton = Button(mainframe, text = "Hangman", command = Hangman.gui) 
hmButton.pack() 

pdButton = Button(mainframe, text = "Poker Dice", command = PokerDice.start) 
pdButton.pack() 

exitButton = Button(mainframe, text = "Quit", command = root.destroy) 
exitButton.pack(side = BOTTOM) 

root.mainloop() 

下面是岩石,纸剪刀代码:

from tkinter import * 

import random 

def gui(): 

    rock = 1 
    paper = 2 
    scissors = 3 

    names = { rock: "Rock", paper: "Paper", scissors: "Scissors" } 
    rules = { rock: scissors, paper: rock, scissors: paper } 

    def start(): 
     while game(): 
      pass 

    def game(): 
     player = playerChoice.get() 
     computer = random.randint(1, 3) 
     computerChoice.set(names[computer]) 
     result(player, computer) 

    def result(player, computer): 
     newScore = 0 
     if player == computer: 
      resultSet.set("Tie game.") 
     elif rules[player] == computer: 
      result.set("Your victory has been assured.") 
      newScore = playerScore.get() 
      newScore += 1 
      playerScore.set(newScore) 
     else: 
      resultSet.set("The commputer laughs as you realise you have " + 
          "been defeated.") 
      newScore = computerScore.get() 
      newScore += 1 
      computerScore.set(newScore) 

    rpsWindow = Toplevel() 
    rpsWindow.title("Rock, Paper, Scissors") 

    playerChoice = IntVar() 
    computerChoice = StringVar() 
    resultSet = StringVar() 
    playerChoice.set(1) 
    playerScore = IntVar() 
    computerScore = IntVar() 

    **rpsFrame = Frame(rpsWindow, padding = '3 3 12 12', width = 300)** 
    #this is what is causing the error 
    rpsFrame.grid(column = 0, row = 0, sticky = (N, W, E, S)) 
    rpsFrame.columnconfigure(0, weight = 1) 
    rpsFrame.rowconfigure(0, weight = 1) 

    Label(rpsFrame, text = 'Player').grid(column = 1, row = 1, sticky = W) 
    Radiobutton(rpsFrame, text = 'Rock', variable = playerChoice, 
       value = 1).grid(column = 1, row = 2, sticky = W) 
    Radiobutton(rpsFrame, text = 'Paper', variable = playerChoice, 
       value = 2).grid(column = 1, row = 2, sticky = W) 
    Radiobutton(rpsFrame, text = 'Scissors', variable = playerChoice, 
       value = 3).grid(column = 1, row = 2, sticky = W) 

    Label(rpsFrame, text = 'Computer').grid(column = 3, row = 3, sticky = W) 
    Label(rpsFrame, textvariable = computerChoice).grid(column = 1, 
                 row = 3, sticky = W) 

    Button(rpsFrame, text = "Play", command = start).grid(column = 2, row = 2) 

    Label(rpsFrame, text = "Score").grid(column = 1, row = 5, sticky = W) 
    Label(rpsFrame, textvariable = playerScore).grid(column = 1, 
                row = 6, sticky = W) 

    Label(rpsFrame, text = "Score").grid(column = 1, row = 5, sticky = W) 
    Label(rpsFrame, textvariable = computerScore).grid(column = 3, 
                 row = 6, sticky = W) 

    Label(rpsFrame, textvariable = resultSet).grid(column = 2, row = 7) 

这里是刽子手代码:

from tkinter import * 
import random 

word = 0 
wordLength = 0 
clue = 0 

def gui(): 
    global word, wordLength, clue 
    dictionary = ["..."] 
    #I've omitted the words due to length 
    word = random.choice(dictionary) 
    wordLength = len(word) 
    clue = wordLength * ["_"] 
    tries = 6 

    def hangedman(hangman): 
     grphic = ["""..."""] 
     #I've omitted the ASCII graphics that go in the list due to length 
     graphicSet = graphic[hangman] 
     hmGraphic.set(graphicSet) 

    def game(): 
     lettersWrong = incorrectGuess.get() 
     letter = guessLetter() 
     firstIndex = word.find(letter) 
     if firstIndex == -1: 
      lettersWrong += 1 
      incorrectGuesses.set(lettersWrong) 
     else: 
      for i in range(wordLength): 
       if letter ++ word[i]: 
        clue[i] = letter 
     hangedman(lettersWrong) 
     clueSet = " ".join(clue) 
     wordOutput.set(clueSet) 
     if lettersWrong == tries: 
      resultText = "Game Over. The word was " + word 
      resultSet.set(resultText) 
      newScore = computerScore.get() 
      newScore += 1 
      computerScore.set(newScore) 
     if "".join(clue) == word: 
      resultText = "You win! The word as " + word 

    def guessLetter(): 
     letter = letterGuess.get() 
     letter = letter.strip() 
     letter = letter.lower() 
     return letter 

    def resetGame(): 
     global word, wordLength, clue 
     incorrectGuesses.set(0) 
     hangedman(0) 
     resultSet.set("") 
     letterGuess.set("") 
     word = random.choice(dictionary) 
     wordLength = len(word) 
     clue = wordLength * ["_"] 
     newClue = " ".join(clue) 
     wordOutput.set(newClue) 

    hmWindow = Toplevel() 
    hmWindow.title("Hangman") 
    incorrectGuesses = IntVar() 
    incorrectGuesses.set(0) 
    playerScore = IntVar() 
    computerScore = IntVar() 
    resultSet = StringVar() 
    letterGuess = StringVar() 
    wordOutput = StringVar() 
    hmGraphic = StringVar() 

    **hmFrame = Frame(hmWindow, padding = "3 3 12 12", width = 300)** 
    #this is what is causing the error 
    hmFrame.grid(column = 0, row = 0, stick = (N, W, E, S)) 
    hmFrame.columnconfigure(0, weight = 1) 
    hmFrame.rowconfigure(0, weight = 1) 

    Label(hmFrame, textvariable = hmGraphic).grid(column = 2, row = 1) 
    Label(hmFrame, text = "Word").grid(column = 2, row = 2) 
    Label(hmFrame, textvariable = wordOutput).grid(column = 2, row = 3) 

    Label(hmFrame, text = "Enter a letter").grid(column = 2, row = 4) 
    hmEntry = Entry(hmFrame, exportselection = 0, 
        textvariable = letterGuess).grid(column = 2, row = 5) 
    hmEntryButton = Button(hmFrame, text = "Guess", 
          command = game).grid(column = 2, row = 10) 

    Label(hmFrame, text = "Wins").grid(column = 1, row = 7, sticky = W) 
    Label(hmFrame, textvariable = playerScore).grid(column = 1, row = 8, 
                sticky = W) 
    Label(hmFrame, text = "Losses").grid(column = 3, row = 7, sticky = W) 
    Label(hmFrame, textvariable = computerScore).grid(column = 3, row = 8, 
                 sticky = W) 
    Label(hmFrame, textvariable = resultSet).grid(column = 2, row = 9) 
    replayButton = Button(hmFrame, text = "Reset", 
          command = resetGame).grid(column = 2, row = 10) 
+0

是的,没有这样的'padding'选项。请参考[这里](http://effbot.org/tkinterbook/frame.htm) –

+0

要复制这个问题,你只需要三行代码;你不需要包括你的整个程序。 –

回答

0

有几乎没有在2.X Tkinter的差异VS 3.x中,除了import语句。这不是2.x和3.x的问题。

padding选项仅适用于ttk.Frame,但您使用的是tkinter.Frame。对于标准tkinter Frame小部件,您有padxpady选项。