Print redirection problem

39 views
Skip to first unread message

Phil

unread,
Jul 11, 2021, 1:00:50 AM7/11/21
to wxpytho...@googlegroups.com
Thank you for reading this.

I'm continuing to have trouble reconciling programming styles that I see
in books with examples that I see online. In an effort to overcome this
problem I have a template that seems to work in most instances yet I
have come unstuck with an example on page 35 from "WxPython in Action
2012" that shows how redirect the output of print statements to the
console or a file.

Print from MyApp is displayed but nothing after that.

import wx

class MyApp(wx.App):
    def __init__(self, redirect=True, filename=None):
        print('App __init__') # This is printed to the console
        super().__init__(self, redirect, filename)

        # init frame
        self.InitFrame()

    def InitFrame(self):
        print('Init Frame')
        frame = MyFrame(parent=None,
                        title="Redirection",
                        size=(300, 300),
                        pos=(100, 100))

        frame.Show()

class MyFrame(wx.Frame):
    def __init__(self, parent, title, size, pos):
        print('Frame __init__')
        super().__init__(parent=parent, title=title, size=(300, 300),
pos=pos)

        self.OnInit()

    def OnInit(self):
        panel = MyPanel(parent=self)

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent=parent)

        wx.StaticText(self, label="Redirect print to panel or file",
pos=(20, 20))

if __name__ == "__main__":
    app = MyApp(redirect=True)#, 'filename')
    app.MainLoop()

The error message is:

App __init__
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/phil/Python/wx_redirection.py", line 37, in <module>
    app = MyApp(redirect=True)#, 'filename')
  File "/home/phil/Python/wx_redirection.py", line 6, in __init__
    super().__init__(self, redirect, filename)
  File "/home/phil/.local/lib/python3.8/site-packages/wx/core.py", line
2175, in __init__
    self.SetUseBestVisual(useBestVisual)
TypeError: PyApp.SetUseBestVisual(): argument 1 has unexpected type
'NoneType'

It would seem that the error is due to something in MyFrame not being
setup correctly but at the moment I cannot see how to solve the problem.

--
Regards,
Phil

Rolf

unread,
Jul 11, 2021, 6:40:13 AM7/11/21
to wxpytho...@googlegroups.com
class MyApp(wx.App):
    def __init__(self, redirect=True, filename=None):
        print('App __init__') # This is printed to the console
        super().__init__(redirect, filename)

Phil

unread,
Jul 12, 2021, 4:39:03 AM7/12/21
to wxpytho...@googlegroups.com
On 11/7/21 8:39 pm, Rolf wrote:
> class MyApp(wx.App):
>     def __init__(self, redirect=True, filename=None):
>         print('App __init__') # This is printed to the console
>         super().__init__(redirect, filename)

Thank you Rolf.

--

Regards,
Phil

Reply all
Reply to author
Forward
0 new messages