Inside of one Python file, I have a wx.Frame class called 'MyFrame,' which simply has a wx.StaticText label called 'label_1.' This file contains a subprocess (irrelevant) which runs the second file.
Inside the second Python file, I have a class that imports 'MyFrame', and contains a function called 'ChangeLabel()' which is supposed to change 'label_1' to display different text.
However, when I run the program, I get the following error:
AttributeError: type object 'MyFrame' has no attribute 'label_1'
I believe the issue has something to do with how I am importing the MyFrame class.
Below is the Code for both python files.
MainFile:
import wx
import subprocess
import sys
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((400, 300))
self.SetTitle("frame")
self.panel_1 = wx.Panel(self, wx.ID_ANY)
sizer_1 = wx.BoxSizer(wx.VERTICAL)
label_1 = wx.StaticText(self.panel_1, wx.ID_ANY, "This is my label")
sizer_1.Add(label_1, 0, 0, 0)
self.panel_1.SetSizer(sizer_1)
##Subprocess that runs "second.py"
result = subprocess.run([sys.executable, "SecondFile.py", "arg"])
self.Layout()
# end wxGlade
# end of class MyFrame
class MyTestApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(self.frame)
self.frame.Show()
return True
# end of class MyTestApp
if __name__ == "__main__":
test_app = MyTestApp(0)
test_app.MainLoop()
SecondFile:
from MainFile import MyFrame
class SecondFile():
def __init__(self, arg):
self.arg = arg
def ChangeLabel():
## Supposed to manipulate MyFrame's label
MyFrame.label_1.SetLabel("New Label Text")
ChangeLabel()