【问题标题】:Class has no attribute 'tk'类没有属性“tk”
【发布时间】:2014-11-07 21:11:27
【问题描述】:

我对使用 GUI(以及一般的 Python)非常陌生,因此我决定尝试我能想到的最困难的事情,以便更好地学习代码。我正在自己从零开始设计一款游戏(这几乎是一个 Pokemon 的翻版,但我无意将其中的任何内容公开,并且所有内容都来自开源软件,例如 bulapedia)。我对 coreEngine 代码还没有太多麻烦;然而,我试着从引擎中休息一下,在 GUI 上工作了一会儿。我决定使用 Tkinter,主要是因为我对 Python 中的 GUI 或任何其他 GUI 模块一无所知,并且在脚本中仅几行就遇到了问题。我的代码如下:

from Tkinter import *
import battleEngine
import openingSequence
from classes import Pokemon
from classes import movesList

def donothing():
    print "Doesn't do anything."

class gameWindow(Frame):
    def displayMenu(self):
        gameMenu = Menu
        fileMenu = Menu(gameMenu, tearoff=0)
        fileMenu.add_command(label="New Game", command=donothing)
        fileMenu.add_command(label="Load Game", command=donothing)
        fileMenu.add_command(label="Save Game", command=donothing)
        fileMenu.add_command(label="Save Game as...", command=donothing)
        fileMenu.add_command(label="Exit", command=exit())

    def __init__(self):
        self.displayMenu()

inGame = gameWindow()

inGame.mainloop()

每当我调用此代码时,都会收到以下错误:

Traceback (most recent call last):
    File "C:/path/to/file/pokemonGui.py", line 26, in <module>
        inGame = gameWindow()
    File "C:/path/to/file/pokemonGui.py", line 24, in __init__
        self.displayMenu()
    File "C:/path/to/file/pokemonGui.py", line 15, in displayMenu
        fileMenu = Menu(gameMenu, tearoff=0)
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 2642, in __init__
        Widget.__init__(self, master, 'menu', cnf, kw)
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 2027, in __init__
        BaseWidget._setup(self, master, cnf)
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 2005, in _setup
        self.tk = master.tk
AttributeError: class Menu has no attribute 'tk'

Process finished with exit code 1

我相信这只是我对 Tkinter 和 GUI 和菜单缺乏了解;但是,我似乎无法弄清楚...我只是想创建一个基本的下拉菜单来浏览屏幕顶部(文件、编辑、查看、帮助等...)但是我不知道该怎么做...

【问题讨论】:

  • gameMenu = Menu 应该是 gameMenu = Menu()。否则 gameMenu 是类本身,而不是其实例

标签: python user-interface tkinter


【解决方案1】:

试试这个:

from Tkinter import *
import battleEngine
import openingSequence
from classes import Pokemon
from classes import movesList

def donothing():
    print "Doesn't do anything."

class gameWindow(Frame):
    def displayMenu(self):
        gameMenu = Menu()
        fileMenu = Menu(gameMenu, tearoff=0)
        fileMenu.add_command(label="New Game", command=donothing)
        fileMenu.add_command(label="Load Game", command=donothing)
        fileMenu.add_command(label="Save Game", command=donothing)
        fileMenu.add_command(label="Save Game as...", command=donothing)
        fileMenu.add_command(label="Exit", command=exit())

    def __init__(self):
        self.displayMenu()

inGame = gameWindow()

inGame.mainloop()

唯一的区别在于第 12 行(包括空行),其中 gameMenu 应该 = Menu()。

【讨论】:

  • 这清除了错误代码;但是,它不会呈现任何输出。我得到一个“进程以退出代码 0 完成”我认为这只会呈现一个顶部有一个菜单的基本窗口(认为它现在只会说“文件”)
  • 那是因为 add_command 的最后一次调用有括号退出所以,只需删除括号
猜你喜欢
  • 2021-11-03
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2019-05-30
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多