Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Trying to use code from WxPython GUI Toolkit
#1
I have copied the following code:
import wx

class Example(wx.Frame()):
	def __init__(self, parent, title):
		super(Example,self).__init__(parent, title = title, size = (300,250))
		self.InitUI()
		self.Centre()
		self.Show()
	def InitUI(self):
		panel = wx.Panel(self)
		hbox = WX.BoxSizer(wx.HORIZONTAL)
		fgs = wx.FlexiGridSizer(3,2,10,10)
		title = wx.StaticText(panel, label = 'Title')
		author = wxStaticText(panel, label = 'Name of the Author')
		review = wxStaticText(panel, label = 'Review')
		tc1 = wx.TextCtrl(panel)
		tc2 = wx.TextCtrl(panel)
		tc3 = wx.TextCtrl(panel, style = wx.TE_MULTILINE)
		fgs.AddMany([(title),(tc1, 1,wx.EXPAND), (author), (tc2,1,wx,EXPAND),(review,1,wx.EXPAND),(tc3,1,wx.EXPAND)])
		fgs.AddGrowableRow(2,1)
		fgs.AddGrowableColumn(1,1)
		hbox.Add(fgs, proportion = 2, flag = wx.ALL|wx.EXPAND, border = 15)
		panel.SetSizer(hbox)


app = wx.App()
Example(None, title='FlexiGrid Demo')

app.MainLoop()
But I get the error:
Error:
Traceback (most recent call last): File "action1.py", line 3, in <module> class Example(wx.Frame()): wx._core.PyNoAppError: The wx.App object must be created first!
All of the code I have looked at so far in the book seems to be set out like this
with the app = wx.App() near the end.
Could anyone show me the correct syntax please?
Quote
#2
(Jun-26-2018, 03:59 PM)Barrowman Wrote: class Example(wx.Frame()):

Are you sure it's wx.Frame(), and not just wx.Frame?
Quote
#3
(Jun-26-2018, 04:32 PM)nilamo Wrote:
(Jun-26-2018, 03:59 PM)Barrowman Wrote: class Example(wx.Frame()):

Are you sure it's wx.Frame(), and not just wx.Frame?

I did think it was but I have had another look at the code in the book and you spotted the ( my ) error.
Thanks for that.
What I started to do about a week ago was to try to develop some code using my laptop screen and a 19" T.V connected to the hdmi port. I did get a program to work but without using classes but it seems most use a class even if it's only used once. So I was trying to adjust my original code to work within a class environment. After messing about with that for some time I thought I should start with less complex stuff until I understand it better.
Quote
#4
wx.Frame is a class. If you don't make your own class, you'll probably end up calling it to build a frame. If you do make your own class, it'll probably use wx.Frame as a parent, which is how your custom class actually works (wx.Frame does the heavy lifting). Having parentheses in a class definition like that looks weird, because it's almost never what you want to be doing (unless it's a function that returns a dynamic class or something, but there's probably better ways to accomplish that anyway).
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [WxPython] Adding a Window to a Button wxPython ShashankDS 4 331 Apr-23-2019, 06:53 PM
Last Post: Yoriz
  WxPython in web with a cgi server jttolleson 11 734 Mar-08-2019, 05:54 PM
Last Post: Weave
  Best GUI toolkit to learn Vicolas 4 412 Jan-24-2019, 07:59 PM
Last Post: Larz60+
  Problems using Wxpython Carles 1 537 Nov-28-2018, 03:09 PM
Last Post: Larz60+
  [WxPython] Using thread in the wxPython hildogjr 8 1,614 Aug-11-2018, 02:59 AM
Last Post: hildogjr
  Using 2 screens in wxpython Barrowman 1 768 Jun-27-2018, 07:43 PM
Last Post: Barrowman
  A little idea to remember wxPython classes Sebastian_Adil 0 785 Mar-26-2018, 10:23 PM
Last Post: Sebastian_Adil
  how to draw simple line in wxPython? Sebastian_Adil 6 1,652 Mar-22-2018, 08:34 PM
Last Post: Larz60+
  installing wxPython predirt 12 3,467 Mar-16-2018, 09:45 PM
Last Post: Barrowman
  [WxPython] wxpython and python 3.6 on linux Barrowman 3 2,578 Mar-15-2018, 06:57 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)