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

  • SEARCH
  • Home
  • 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 7703159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:23:39+00:00 2026-05-31T23:23:39+00:00

I have never code in Python and trying to switch from Javascrpt/SVG. Being confused

  • 0

I have never code in Python and trying to switch from Javascrpt/SVG. Being confused by variable scope in Python and process flow, I will appreciate any correction to those basic code to make it draw rectangle by mousedown and mouseup events. Please don’t put links to instructions unless you didn’t point me on errors in code.

if name==”main“:
import wx
import math

class myframe(wx.Frame):
    pt1 = 0
    pt2 = 0
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "test", size=(500,400))
        self.Bind(wx.EVT_LEFT_DOWN, self.onDown)
        self.Bind(wx.EVT_LEFT_UP, self.onUp)
        self.Bind(wx.EVT_PAINT, self.drawRect)

    def onDown(self, event):          
        global pt1
        pt1 = event.GetPosition() # firstPosition tuple

    def onUp(self, event):          
        global pt2
        pt2 = event.GetPosition() # secondPosition tuple

    def drawRect(self, event):
        dc = wx.PaintDC(self)
        gc = wx.GraphicsContext.Create(dc)
        nc = gc.GetNativeContext()
        ctx = Context_FromSWIGObject(nc)

        ctx.rectangle (pt1.x, pt1.y, pt2.x, pt2.y) # Rectangle(x0, y0, x1, y1)
        ctx.set_source_rgba(0.7,1,1,0.5)
        ctx.fill_preserve()
        ctx.set_source_rgb(0.1,0.5,0)
        ctx.stroke()


app = wx.App()
f = myframe()
f.Show()
app.MainLoop()
  • 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. Editorial Team
    Editorial Team
    2026-05-31T23:23:40+00:00Added an answer on May 31, 2026 at 11:23 pm

    Yeh, you have a problem with scopes (plus – your code isn’t showing properly).

    Let me give you a short example how to use members and globals in python:

    # Globals are defined globally, not in class
    glob1 = 0
    
    class C1:
        # Class attribute
        class_attrib = None  # This is rarely used and tricky
    
        def __init__(self):
            # Instance attribute
            self.pt1 = 0  # That's the standard way to define attribute
    
        def other_method(self):
            # Use of a global in function
            global glob1
            glob1 = 1
    
            # Use of a member
            self.pt1 = 1
    
    # Use of a class attribute
    C1.class_attrib = 1
    

    In your code you are mixing all types of variables. I think you should just make pt1 and pt2 instance attributes, so your code would look like:

    class MyFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, -1, "test", size=(500,400))
            self.pt1 = self.pt2 = 0
            ...
    
        def onDown(self, event):          
            self.pt1 = event.GetPosition() # firstPosition tuple
    
        ...
    

    You could consider reading some general tutorial like this one, to learn how Python scoping works.

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

Sidebar

Related Questions

I have never programed in Python before, so excuse my code. I have this
I have never used Try-catch in my code before, but now I need to
I have never seen the usecase for pre-increment and post-increment in actual code. The
I have following code that does not work: I never get to goToFoodDetail .
Never thought I'd have this problem :) The following snippet of code works in
I'm trying to simulate a token ring using python with sockets, but I have
i'm python newbie, and member variable of class works weird in my python code.
I have never used Python before, most of my programming has been in MATLAB
Im trying to read in image file from a server , with the code
My Python is worse than my Chinese (I have never seen so many smiling

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.