I have run the slider demo that come with wxPython docs. It is rather stupid, as it puts slider on a panel using many unexplained numbers, never tells how to get position of slider or what to do next if the slider is moved. I am much lost, does anyone have a actual usefull example?

Recommended Answers

All 13 Replies

I copied the snippet onto my IDE editor and it works well. Then I made three of vertically sliders and just chnaged the upper left corner x,y of each so now I have three sliders next to each other in the right side of frame or panel for RGB selection. Looks like good start of project.

What would you recommend to use for a colour area. I need two of them one for computer select colour and one for slider select colour.

when I run the code, I get this error

shane@mainbox ~ $ python test.py
Traceback (most recent call last):
  File "test.py", line 58, in ?
    MyPanel(frame,-1)
  File "test.py", line 41, in __init__
    self.Bind(wx.EVT_SLIDER, self.sliderUpdate)
AttributeError: MyPanel instance has no attribute 'Bind'

My linux system has two different versions of wxPython installed, my guess is it is using the wrong version.

Bind was introduced with version 2.5 of wxPython.

is version 2.4 of wxPython still needed for many apps to run. Do you guys run both versions?

The newest wxPython version 2.6 is still compatible with 2.4 even though the wx namespace is now preferred. If you use an editor with code completion, then you get a dropdown list of wxPython choices after you typed "wx." .

I would not recommend to keep old versions of wxPython around, mixed with newer versions!

I copied the snippet onto my IDE editor and it works well. Then I made three of vertically sliders and just chnaged the upper left corner x,y of each so now I have three sliders next to each other in the right side of frame or panel for RGB selection. Looks like good start of project.

What would you recommend to use for a colour area. I need two of them one for computer select colour and one for slider select colour.

The best area to color with the sliders without getting too much flickering would be a paint event like drawing and then floodfilling a shape like a rectangle. Take a look at the wxPython snippet ...
http://www.daniweb.com/code/snippet407.html

This how far I am with the program. It does flicker so and needs more work:

# Colour Matching Skill Tester    first version
# Colour the label1 with random RGB values
# Colour the label2 with RGB values from sliders
# Needs exposition of result closeness and possible file saving

import random
import wx

class MyPanel(wx.Panel):
    """ class MyPanel creates a panel with 2 labels, 3 sliders, inherits wx.Panel """
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.SetBackgroundColour("white")
        str1 = ""
        
        self.Red1 = random.randrange(256)
        self.Green1 = random.randrange(256)
        self.Blue1 = random.randrange(256)
        # label1 with random RGB colour values
        self.label1 = wx.StaticText(self, -1, str1 , (10, 10), (120, 100))
        self.label1.SetBackgroundColour((self.Red1, self.Green1, self.Blue1))
        
        self.Red2 = 125
        self.Green2 = 125
        self.Blue2 = 125
        # label2 with RGB colour values from sliders
        self.label2 = wx.StaticText(self, -1, str1 , (10, 120), (120, 100))
        self.label2.SetBackgroundColour((self.Red2, self.Green2, self.Blue2))
        
        # init value = 125, min value = 0, max value = 255 for colours
        self.sliderR = wx.Slider(self, -1, self.Red2, 0, 255, (140, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Red" , (160, 205))
        self.sliderG = wx.Slider(self, -1, self.Green2, 0, 255, (200, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Green" , (220, 205))
        self.sliderB = wx.Slider(self, -1, self.Blue2, 0, 255, (260, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Blue" , (280, 205))
        
        # result label
        self.label3 = wx.StaticText(self, -1, "", (160, 230))

        # all three sliders ...
        self.Bind(wx.EVT_SCROLL, self.slideColour)
       
        # button click
        self.button1 = wx.Button(self, -1, "Evaluate", (10, 230))
        self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
        
    def slideColour(self, event):
        self.Red2 = self.sliderR.GetValue()
        self.Green2 = self.sliderG.GetValue()
        self.Blue2 = self.sliderB.GetValue()
        colour1 = wx.Colour(self.Red2, self.Green2, self.Blue2)
        # still flickers much!
        self.label2 = wx.StaticText(self, -1, "", (10, 120), (120, 100)).SetBackgroundColour(colour1)
     
    def button1Click(self,event):
        str1 = "Upper  R=%d G=%d B=%d" % (self.Red1, self.Green1, self.Blue1)
        str2 = "Lower  R=%d G=%d B=%d" % (self.Red2, self.Green2, self.Blue2)
        self.label3.SetLabel(str1 + '\n' + str2)
        

app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "Colour Skill Tester V1.0 HAB", size = (350, 310))
MyPanel(frame,-1)
frame.Show(True)
app.MainLoop()

Anybody have idea about stopping flicker?

Try to update the coulor field only on the OnEraseBackground event.

Something like this :

in def _init_ :

self.panel.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

and then a extra def with the function for updating the colour field.

def OnEraseBackground(self, evt):
etc.....


Hope this helps.

Regards,
Peter

Peter thank you much,

I am just learning Python and need some handholding please! Can you give more detail?


Why do I use panel in bind?

What do I code in function "def OnEraseBackground(self, evt):"?

I have got a simpeler solution :

# Colour Matching Skill Tester    first version
# Colour the label1 with random RGB values
# Colour the label2 with RGB values from sliders
# Needs exposition of result closeness and possible file saving

import random
import wx

class MyPanel(wx.Panel):
    """ class MyPanel creates a panel with 2 labels, 3 sliders, inherits wx.Panel """
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.SetBackgroundColour("white")
        str1 = ""
        
        self.Red1 = random.randrange(256)
        self.Green1 = random.randrange(256)
        self.Blue1 = random.randrange(256)
        # label1 with random RGB colour values
        self.label1 = wx.StaticText(self, -1, str1 , (10, 10), (120, 100))
        self.label1.SetBackgroundColour((self.Red1, self.Green1, self.Blue1))
        
        self.Red2 = 125
        self.Green2 = 125
        self.Blue2 = 125
        # label2 with RGB colour values from sliders
        self.label2 = wx.StaticText(self, -1, str1 , (10, 120), (120, 100))
        self.label2.SetBackgroundColour((self.Red2, self.Green2, self.Blue2))
        
        # init value = 125, min value = 0, max value = 255 for colours
        self.sliderR = wx.Slider(self, -1, self.Red2, 0, 255, (140, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Red" , (160, 205))
        self.sliderG = wx.Slider(self, -1, self.Green2, 0, 255, (200, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Green" , (220, 205))
        self.sliderB = wx.Slider(self, -1, self.Blue2, 0, 255, (260, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Blue" , (280, 205))
        
        # result label
        self.label3 = wx.StaticText(self, -1, "", (160, 230))

        # all three sliders ...
        self.Bind(wx.EVT_SCROLL, self.slideColour)
       
        # button click
        self.button1 = wx.Button(self, -1, "Evaluate", (10, 230))
        self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
        
    def slideColour(self, event):
        self.Red2 = self.sliderR.GetValue()
        self.Green2 = self.sliderG.GetValue()
        self.Blue2 = self.sliderB.GetValue()
        colour1 = wx.Colour(self.Red2, self.Green2, self.Blue2)
    
        # still flickers much!
        # self.label2 = wx.StaticText(self, -1, "", (10, 120), (120, 100)).SetBackgroundColour(colour1)
        
        # This does the trick !!!
        self.label2.ClearBackground()
        self.label2.SetBackgroundColour(colour1) 
        

    
    def button1Click(self,event):
        str1 = "Upper  R=%d G=%d B=%d" % (self.Red1, self.Green1, self.Blue1)
        str2 = "Lower  R=%d G=%d B=%d" % (self.Red2, self.Green2, self.Blue2)
        self.label3.SetLabel(str1 + '\n' + str2)
        

app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "Colour Skill Tester V1.0 HAB", size = (350, 310))
MyPanel(frame,-1)
frame.Show(True)
app.MainLoop()

Peter.

Wow Peter ClearBackground() did the trick and without it the colour would not chnage at all. Looks like you have to clear the old colour out first.

I could not find ClearBackground() at all in any information I have so far about wxPython! I do wonder where things like that are hidden at? Anyway thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.