【问题标题】:Open file and create new frame打开文件并创建新框架
【发布时间】:2014-05-04 13:29:04
【问题描述】:

我想打开 .py 文件——我发现了——然后从打开的文件运行新框架。这是文件 MenuWindow 中的代码:

def OnNewGame(self,event):
    play = False
    nameofplr = wx.GetTextFromUser('Enter your name:', 'NEW GAME')
    subor=open("Save/savegame.txt","a")
    subor.write( "\n" + nameofplr)
    subor.close()
    savegame=open("Save/" + nameofplr + ".prjct", "w+")
    savegame.close()
    play = True
    if (play == True):
        sys.path.insert(0, './bin')
        import game
        new = GameFrame(parent=None, id=-1)
        new.Show()
        self.Close()

这是来自 game.py 的代码:

    print "game.py opened"

导入 wx

类 GameFrame(wx.Frame):

def __init__(self,parent,id):

    wx.Frame.__init__(self, parent, id, "Project", size=(860, 640))
    wx.Frame.CenterOnScreen(self)

输出打印 game.py 打开所以它打开文件,但为什么它不创建新框架?
这是输出:

game.py 打开
回溯(最近一次通话最后一次):
文件“D:\Python\Python Projects\Project\MenuWindow.py”,第 48 行,在 OnNewGame
新 = GameFrame(父=None, id=-1)
NameError:未定义全局名称“GameFrame”

【问题讨论】:

    标签: file wxpython frame


    【解决方案1】:

    你导入游戏,GameFrame是游戏的一部分,所以你应该输入

    new = game.GameFrame(parent=None, id=-1)
    

    【讨论】:

      【解决方案2】:

      我在互联网上搜索了大约 2 或 3 个小时,但没有任何效果,然后我在这里写问题并尝试修改我自己的代码,我已经修复了它!所以如果有人遇到同样的问题,这里是 def OnNewGame 的代码:

          def OnNewGame(self,event):
          play = False
          nameofplr = wx.GetTextFromUser('Enter your name:', 'NEW GAME')
          subor=open("Save/savegame.txt","a")
          subor.write( "\n" + nameofplr)
          subor.close()
          savegame=open("Save/" + nameofplr + ".prjct", "w+")
          savegame.close()
          play = True
          if (play == True):
              sys.path.insert(0, './bin')
              from game import GameFrame
              new = GameFrame(parent=None, id=-1)
              new.Show()
              self.Close()
      

      仅此而已!!!

      【讨论】:

        猜你喜欢
        • 2015-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-07
        • 2022-10-26
        • 2013-03-07
        • 1970-01-01
        相关资源
        最近更新 更多