What is the correct way to link a button to open an other py file?

I’ve a big doubt because my script is not performing as it should be. So I’ve a simple main file with a button that opens a blank grid (codes below). The problem with this code is that it opens reportWindow the first time it executes, but if I close the report andI try to open it again I receive the error :
NameError: name ‘TestFrame’ is not defined

I’ve also removed “if name == 'main’” from the last lines of reportWindow.py because the script wasn’t working with it. I tried “if name == ‘main’” as it’s imported from main.py but it didn’t work.

Please, can someone show me how it should have been done in the first place?

Thank you

main.py

import wx

class Test(wx.Frame):

def init(self,parent,id):

wx.Frame.init(self,parent,id, “Frame aka Window”, size=(300, 200))

panel = wx.Panel(self)

button = wx.Button(panel, label = “Exit”, pos=(80, 80), size = (120,30))

self.Bind(wx.EVT_BUTTON, self.closebutton, button)

def closebutton(self,event):

from reportWindow import SimpleGrid

SimpleGrid(TestFrame, -1)

if name == ‘main’:

app = wx.App()

frame = Test(parent=None, id=1)

frame.Show()

app.MainLoop()

reportWindow.py

import wx

import wx.grid as gridlib

class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):

def init(self, parent, log):

gridlib.Grid.init(self, parent, -1)

#[…Some code…]

class TestFrame(wx.Frame):

def init(self, parent, log):

wx.Frame.init(self, parent, 0, “Title”, size=(1400,800))

self.grid = SimpleGrid(self, log)

#[…Some code…]

#if name == ‘main’:

import sys

from wx.lib.mixins.inspection import InspectableApp

app = InspectableApp(False)

frame = TestFrame(None, sys.stdout)

frame.Show(True)

#import wx.lib.inspection

#wx.lib.inspection.InspectionTool().Show()

app.MainLoop()

Yep, that’s correct. Where did you think that name was coming from?

I suspect you just need to add

from reportWindow import TestFrame, SimpleGrid

at the top of main.py.

···

On May 25, 2019, at 7:04 PM, Youn-Bo younes.boutriq@gmail.com wrote:

I’ve a big doubt because my script is not performing as it should be. So I’ve a simple main file with a button that opens a blank grid (codes below). The problem with this code is that it opens reportWindow the first time it executes, but if I close the report andI try to open it again I receive the error :
NameError: name ‘TestFrame’ is not defined


Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Unfortunately, it’s not working. I suspect the line SimpleGrid(TestFrame, -1)

···

On Sunday, 26 May 2019 02:27:35 UTC-4, Tim Roberts wrote:

On May 25, 2019, at 7:04 PM, Youn-Bo younes...@gmail.com wrote:

I’ve a big doubt because my script is not performing as it should be. So I’ve a simple main file with a button that opens a blank grid (codes below). The problem with this code is that it opens reportWindow the first time it executes, but if I close the report andI try to open it again I receive the error :
NameError: name ‘TestFrame’ is not defined

Yep, that’s correct. Where did you think that name was coming from?

I suspect you just need to add

from reportWindow import TestFrame, SimpleGrid

at the top of main.py.


Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Just follow the
above but instead of a menu item use a button.

Johnf

···

https://www.blog.pythonlibrary.org/2018/10/19/wxpython-how-to-open-a-second-window-frame/
On 5/25/19 7:04 PM, Youn-Bo wrote:

    I've a big doubt because my script is not

performing as it should be. So I’ve a simple main file with a
button that opens a blank grid (codes below). The problem with
this code is that it opens reportWindow the first time it
executes, but if I close the report andI try to open it again I
receive the error :
*NameError: name ‘TestFrame’ is not defined
*

I’ve also removed “if name == 'main’” from
the last lines of reportWindow.py because the script wasn’t
working with it. I tried "if name == ‘main’ " as
it’s imported from main.py but it didn’t work.

      Please, can someone show me how it should have been done in

the first place?

Thank you

**main.py
**

import wx

class Test(wx.Frame):

def init(self,parent,id):

wx.Frame.init(self,parent,id, “Frame aka Window”, size=(300, 200))

panel = wx.Panel(self)

button = wx.Button(panel, label = “Exit”, pos=(80, 80), size = (120,30))

self.Bind(wx.EVT_BUTTON, self.closebutton, button)

def closebutton(self,event):

from reportWindow import SimpleGrid

SimpleGrid(TestFrame, -1)

if name == ‘main’:

app = wx.App()

frame = Test(parent=None, id=1)

frame.Show()

app.MainLoop()

reportWindow.py

import wx

import wx.grid as gridlib

class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):

def init(self, parent, log):

gridlib.Grid.init(self, parent, -1)

#[…Some code…]

class TestFrame(wx.Frame):

def init(self, parent, log):

wx.Frame.init(self, parent, 0, “Title”, size=(1400,800))

self.grid = SimpleGrid(self, log)

#[…Some code…]

#if name == ‘main’:

import sys

from wx.lib.mixins.inspection import InspectableApp

app = InspectableApp(False)

frame = TestFrame(None, sys.stdout)

frame.Show(True)

#import wx.lib.inspection

#wx.lib.inspection.InspectionTool().Show()

app.MainLoop()

  You received this message because you are subscribed to the Google

Groups “wxPython-users” group.

  To unsubscribe from this group and stop receiving emails from it,

send an email to wxpython-users+unsubscribe@googlegroups.com.

  To view this discussion on the web visit [https://groups.google.com/d/msgid/wxpython-users/eaa07d3d-4547-41e0-bc66-9c296cf03bbd%40googlegroups.com](https://groups.google.com/d/msgid/wxpython-users/eaa07d3d-4547-41e0-bc66-9c296cf03bbd%40googlegroups.com?utm_medium=email&utm_source=footer).

  For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

TestFrame is a class. SimpleGrid wants an instance of a class to use as a parent window. IOW, you need to create a TestFrame.

···

On Saturday, May 25, 2019 at 11:44:36 PM UTC-7, Youn-Bo wrote:

Unfortunately, it’s not working. I suspect the line SimpleGrid(TestFrame, -1)

Robin