Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 94433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:30:03+00:00 2026-05-10T23:30:03+00:00

I’ve been working with python for a while now and am just starting to

  • 0

I’ve been working with python for a while now and am just starting to learn wxPython. After creating a few little programs, I’m having difficulty understanding how to create objects that can be shared between dialogs.

Here’s some code as an example (apologies for the length – I’ve tried to trim):

import wx  class ExampleFrame(wx.Frame):     '''The main GUI'''     def __init__(self, parent, id, title):         wx.Frame.__init__(self, parent, id, title, size=(200,75))         mainSizer = wx.BoxSizer(wx.VERTICAL)          # Setup buttons         buttonSizer = wx.BoxSizer(wx.HORIZONTAL)         playerButton = wx.Button(self, wx.ID_ANY, 'Player number', wx.DefaultPosition, wx.DefaultSize, 0)         buttonSizer.Add(playerButton, 1, wx.ALL | wx.EXPAND, 0)         nameButton = wx.Button(self, wx.ID_ANY, 'Player name', wx.DefaultPosition, wx.DefaultSize, 0)         buttonSizer.Add(nameButton, 1, wx.ALL | wx.EXPAND, 0)          # Complete layout and add statusbar         mainSizer.Add(buttonSizer, 1, wx.EXPAND, 5)         self.SetSizer(mainSizer)         self.Layout()          # Deal with the events         playerButton.Bind(wx.EVT_BUTTON, self.playerButtonEvent)         nameButton.Bind(wx.EVT_BUTTON, self.nameButtonEvent)         self.Show(True)         return      def playerButtonEvent(self, event):         '''Displays the number of game players'''         playerDialog = PlayerDialogWindow(None, -1, 'Player')         playerDialogResult = playerDialog.ShowModal()          playerDialog.Destroy()         return      def nameButtonEvent(self, event):         '''Displays the names of game players'''         nameDialog = NameDialogWindow(None, -1, 'Name')         nameDialogResult = nameDialog.ShowModal()          nameDialog.Destroy()         return  class PlayerDialogWindow(wx.Dialog):     '''Displays the player number'''     def __init__(self, parent, id, title):         wx.Dialog.__init__(self, parent, id, title, size=(200,120))          # Setup layout items         self.SetAutoLayout(True)         mainSizer = wx.BoxSizer(wx.VERTICAL)         dialogPanel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)         dialogSizer = wx.BoxSizer(wx.VERTICAL)          # Display player number         playerNumber = 'Player number is %i' % gamePlayer.number         newLabel = wx.StaticText(dialogPanel, wx.ID_ANY, playerNumber, wx.DefaultPosition, wx.DefaultSize, 0)         dialogSizer.Add(newLabel, 0, wx.ALL | wx.EXPAND, 5)          # Setup buttons         buttonSizer = wx.StdDialogButtonSizer()         okButton = wx.Button(dialogPanel, wx.ID_OK)         buttonSizer.AddButton(okButton)         buttonSizer.Realize()         dialogSizer.Add(buttonSizer, 1, wx.EXPAND, 5)          # Complete layout         dialogPanel.SetSizer(dialogSizer)         dialogPanel.Layout()         dialogSizer.Fit(dialogPanel)         mainSizer.Add(dialogPanel, 1, wx.ALL | wx.EXPAND, 5)         self.SetSizer(mainSizer)         self.Layout()          # Deal with the button events         okButton.Bind(wx.EVT_BUTTON, self.okClick)         return      def okClick(self, event):         '''Deals with the user clicking the ok button'''         self.EndModal(wx.ID_OK)         return   class NameDialogWindow(wx.Dialog):     '''Displays the player name'''     def __init__(self, parent, id, title):         wx.Dialog.__init__(self, parent, id, title, size=(200,120))          # Setup layout items         self.SetAutoLayout(True)         mainSizer = wx.BoxSizer(wx.VERTICAL)         dialogPanel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)         dialogSizer = wx.BoxSizer(wx.VERTICAL)          # Display player number         playerNumber = 'Player name is %s' % gamePlayer.name         newLabel = wx.StaticText(dialogPanel, wx.ID_ANY, playerNumber, wx.DefaultPosition, wx.DefaultSize, 0)         dialogSizer.Add(newLabel, 0, wx.ALL | wx.EXPAND, 5)          # Setup buttons         buttonSizer = wx.StdDialogButtonSizer()         okButton = wx.Button(dialogPanel, wx.ID_OK)         buttonSizer.AddButton(okButton)         buttonSizer.Realize()         dialogSizer.Add(buttonSizer, 1, wx.EXPAND, 5)          # Complete layout         dialogPanel.SetSizer(dialogSizer)         dialogPanel.Layout()         dialogSizer.Fit(dialogPanel)         mainSizer.Add(dialogPanel, 1, wx.ALL | wx.EXPAND, 5)         self.SetSizer(mainSizer)         self.Layout()          # Deal with the button events         okButton.Bind(wx.EVT_BUTTON, self.okClick)         return      def okClick(self, event):         '''Deals with the user clicking the ok button'''         self.EndModal(wx.ID_OK)         return   class Player(object):     '''A game player'''     def __init__(self, number, name):         self.number = number         self.name = name         return  def main():     # Start GUI     global gamePlayer     gamePlayer = Player(1, 'John Smith')     app = wx.App(redirect=False)     frame = ExampleFrame(None, -1, 'Example frame')     frame.Show(True)     app.MainLoop()     return 0  if __name__ == '__main__':     main() 

So, I want both of the dialogs to access the gamePlayer object. At the moment, the only way I can think of doing it is to create the gamePlayer object as a global object, but these are normally frowned upon – is there a better way to do this?

There is a method of passing objects in event bindings in this question, but it doesn’t feel quite right.

Is learning to implement the MVC pattern the way forward here?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:30:03+00:00Added an answer on May 10, 2026 at 11:30 pm

    You can pass a gamePlayer object to __init__ as another argument.

    def __init__(self, parent, id, title, gamePlayer ):     ...etc... 

    In the long run, this isn’t ideal.

    You should separate building an empty panel from loading that panel with data. The empty panel is one thing, populating it with data from the model is unrelated.

    Populating a frame with data is where you must be given the gamePlayer object which will be used to update the various display widgets.

    I’d suggest you look at the Document-View framework for guidance on this. http://docs.wxwidgets.org/stable/wx_docviewoverview.html#docviewoverview. Unfortunately, there aren’t any good Python examples of this, so it can be confusing to convert from the C++ code to Python.

    Ultimately, you have a ‘document’ which is the main object (‘gamePlayer’) that is being displayed. Each Frame is a view of that document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer class Program { static void Main(string[] args) { Timer timer… May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Use GROUP_CONCAT SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo; May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Your code doesn't work because the function is not returning… May 11, 2026 at 10:50 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.