Question

I want to open .py file - that i have found out - and than from opened file run new frame. This is code in file 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()

And This is code from game.py:

    print "game.py opened"

import wx

class GameFrame(wx.Frame):

def __init__(self,parent,id):

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

Output prints game.py opened so it open the file, but why it doesn´t create new frame?
This is output:

game.py opened
Traceback (most recent call last):
File "D:\Python\Python Projects\Project\MenuWindow.py", line 48, in OnNewGame
new = GameFrame(parent=None, id=-1)
NameError: global name 'GameFrame' is not defined

Was it helpful?

Solution

You import game, and GameFrame is a part of game, so you should type

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

OTHER TIPS

I was searching on internet for about 2 or 3 hours and nothing worked, then i write question here and tried modify code of my own and i have fixed it! So if somebody will have the same problem here is the code for 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()

An that´s all it works!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top