#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Question Global veriables


    hi i am new to the hole forum thing so tell me if i am doing this wrong but i have been stuck on a piece of code for a really long time and i dont know how to make the veriable avalable to multiple classes if that makes any sense what so ever i am using both wx and pygame in my programme altho i dont think that has any effect thank you to an one who can help

    (i would like to get the slid veriables from __init__ to the colour veriales in if name = main please help )

    Code:
    import wx
    import pygame , sys
    from pygame.locals import *
    
    slid=0
    slid2=0
    slid3=0
    
    class andrew(wx.Frame):
    
        def __init__(self,parent,id, (slid,slid2,slid3)):
            wx.Frame.__init__(self,parent,id,'colour sliders', size=(300,500))
            panel=wx.Panel(self)
            button=wx.Button(panel,label="ok",pos=(150,200),size=(60,60))
            self.Bind(wx.EVT_BUTTON,self.closebutton, button)
            self.Bind(wx.EVT_CLOSE,self.closewindow)
    
            
    
            slider=wx.Slider(panel, -1, 50, 0 ,255, pos=(10,10), size=(250,-1), style=wx.SL_AUTOTICKS | wx.SL_LABELS)
            slider.SetTickFreq(5, 1)
            slider2=wx.Slider(panel, -1, 50, 0 ,255, pos=(10,70), size=(250,-1), style=wx.SL_AUTOTICKS | wx.SL_LABELS)
            slider2.SetTickFreq(5, 1)
            slider3=wx.Slider(panel, -1, 50, 0 ,255, pos=(10,130), size=(250,-1), style=wx.SL_AUTOTICKS | wx.SL_LABELS)
            slider3.SetTickFreq(5, 1)
            slid=slider.GetValue()
            slid2=slider2.GetValue()
            slid3=slider3.GetValue()
            
            
            
            
    
            
         
    
        def closebutton(self,event):
            self.Close(True)
             
        def closewindow(self,event):
            self.Destroy()
    
    if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=andrew(parent=None,id=-1)
        frame.Show()
        app.MainLoop()
        
    class circle:
     pygame.init()
    
    
    
     screen=pygame.display.set_mode((640,360),0,32)
     colour=(slid,slid2,slid3)
     pos=(320,176)
     rad=(70)
    
     while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
    
        screen.lock()
        pygame.draw.circle(screen,colour,pos,rad)
        screen.unlock()
    
    
        pygame.display.update()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Put your code between [ code ] tags so that proper indentation is preserved. This is pretty essential with python code. See the "Asking for help?" sticky post for more information.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    thank you where is the ask for help sticknote located ?(nvm)
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Present python code in this forum with code tags according to my signature. Investigate the global statement or perhaps try a trick like this:
    Code:
    class base:
    
        # group some somewhat global globals into something like FORTRAN common block
    
        slid = 'slid in base'
    
        def __repr__(self):
            return 'slid is '+base.slid
    
    
    class sub1(base):
    
        def __init__(self,x):
            super().__init__()        # python 3 statement.
            #base.__init__(self)      # alternative for python 2
            self.x = 'x in sub1'
    
    
    class sub2(base):
    
        def __init__(self):
            base.__init__(self)       # python 2 statement.
            #super().__init__()       # alternative for python 3
            base.slid = 'base.slid changed by class sub2'
    
    
    o1 = sub1(8)
    print(o1)        # shows   slid is slid in base
    
    
    o2 = sub2()
    print(o1)        # shows   slid is base.slid changed by class sub2
    print(o2)        # shows   slid is base.slid changed by class sub2
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    thank you altho this is a bit over whelming as i am increadably new to programming and alot of that comfuses me but thank you

IMN logo majestic logo threadwatch logo seochat tools logo