RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Display a Meter

Sat Mar 04, 2017 7:09 pm

I have a Python program that monitors the output of Solar Panels.
I would like to present the output on screen to look like a Meter, with a scale of 0 - 4.

Is this possible in Python?

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Display a Meter

Sun Mar 05, 2017 9:42 am

There is some code in this link which might help.

viewtopic.php?f=38&t=134854#p897766

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Display a Meter

Mon Mar 06, 2017 7:33 am

@RogerW
Thank you very much, that looks very close to what I was hoping for.
I have started to make some changes but eventually I would like:
- The needle to start Horizontally on the left and move through 180 degrees
- To remove the slider from the bottom
- To have just one meter
- Take my inputs for the meter from my existing python program.

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Display a Meter

Mon Mar 06, 2017 10:02 am

Glad to help. The first file (meter.py) contains the meter class. The second file (trymeter.py) is just a testbed. You can write your own tkinter program to include a meter object and drive it any way you like.

It should not be too difficult to modify the meter class to have the needle start horizontal. Look at the two functions set (which draws the needle) and graphics which draws the ticks. You may have to do more but that should give you a start.

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Display a Meter

Mon Mar 06, 2017 11:28 am

@RogerW
I have altered the start and end position of the ticks and I have moved the counter display to the bottom half, because I will only be using the top half of the display for the needle.
I have not yet found how to alter the needle start and end position, how to delete the horizontal slider, how to just have one meter or (more importantly) how to take an input from one of my variables to drive the needle. I would also like to display the counter with 3 decimal places.

Reading that list, I have not made much progress at all yet but still working on it.

Thanks again though, this is a great start.

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Display a Meter

Mon Mar 06, 2017 11:51 am

In meter.py make this change in the function set.

Code: Select all

        # call this to set the hand
        # convert value to range 0,100
        #deg = 300*(value - self.start)/self.range - 240
        deg = 180*(value - self.start)/self.range - 180
 
I think you need to understand how tkinter works and (maybe) get used to using class objects.
Trymeter.py has a class Mainframe. This creates two Meterframe objects - I think you only need one. The Meterframe class (object) contains the meter (which you need) a Label which you might not need and a slider which you do not want. You need to get rid of the slider and provide some other mechanism to set the meter.

You will probably find the tkinter function after useful. It allows you to set a function to be called after a given number of milliseconds - you can use this to update the meter on a regular basis.

You may find this useful.
viewtopic.php?f=32&t=117010&p=1120565#p1120565

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Display a Meter

Mon Mar 06, 2017 7:22 pm

@RogerW
Thank you for your continued support.
I am going to take your advice and learn about classes. I am looking for some suitable material.
My favourite Python text book so far is 'Python makes Programming fun', by Mike McGrath but the section on classes although quite extensive, is not helping me so far. I will persevere though.

Incidentally, for my meter display, I decided on a display of just over 180 degrees in order to split nicely into 40 (4 x 10) segments.
I have finished up with the following code in the graphics function, which works well:
for deg in range (-10,194,5):
self.createtick(deg,self.minortick)
for deg in range (-10,200,50):
self.createtick(deg,self.majortick)

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Display a Meter

Tue Mar 07, 2017 10:15 am

I think time spent getting to grips with classes will be well spent although others here might disagree.
My view is that they come into there own as your program becomes more complicated. If you only want one meter then separating the code into a separate class may be questionable but when you want 10 of them the benefits become clearer. Also it is easy to re use code when you write another program that needs a meter.

Please do play with the code - you will get to understand it better that way and you need to make it do what you want rather than what I had in mind when I wrote it.

I am afraid I cannot advise on books - I learnt about object oriented programming years ago using C++ and am largely self taught on python. As you become more confident you can use the reference material.

Python is still evolving but this should be good enough for now.
https://docs.python.org/3.3/

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Display a Meter

Tue Mar 07, 2017 1:23 pm

The main problem I have having at the moment is understanding what Classes are and when to use them. I have read a few different explanations but I am yet to find one that convinces me.
There are also some lines in the meter.py program beginning with a 'comma' and I have so far been unable to find anything that explains the use of the comma.

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Display a Meter

Tue Mar 07, 2017 2:04 pm

There are times when a line gets too long to be convenient. You can break the line to make it more readable. There are rules to say when you can do this. One of the times you can is with expressions inside brackets. I choose to do this by breaking the line just before the comma but that is just my convention.

I find explaining classes more difficult - maybe there is a real computer scientist round here that can help. You are in fact using class objects all the time. Python uses classes for everything. If you say
x = 1
python creates a class object, calls it x and stores 1 in it. When you define a class object using the keyword 'class' you define what it can do (by defining functions) and the data stored in it. The keyword self indicates a variable or function within the class object.

I hope this helps.

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: Display a Meter

Tue Mar 07, 2017 3:30 pm

RogerW wrote:There are times when a line gets too long to be convenient. You can break the line to make it more readable. There are rules to say when you can do this. One of the times you can is with expressions inside brackets. I choose to do this by breaking the line just before the comma but that is just my convention.
PEP8, the python style guide suggests that you should increase the indent of the code when you continue on to a new line.

For example, your code

Code: Select all

    def graphics(self):
        # create the static components
        self.create_oval(self.centrex-self.radius
        ,self.centrey-self.radius
        ,self.centrex+self.radius
        ,self.centrey+self.radius
        ,width = self.bezel
        ,outline = self.bezelcolour2)
Re-formatted as per PEP8

Code: Select all

    def graphics(self):
        # create the static components
        self.create_oval(self.centrex - self.radius,
                         self.centrey - self.radius,
                         self.centrex + self.radius,
                         self.centrey + self.radius,
                         width=self.bezel,
                         outline=self.bezelcolour2)
This helps to reduce any ambiguity about whether the new line is a new command with a comma incorrectly placed or a continuation of the previous line.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Display a Meter

Tue Mar 07, 2017 7:37 pm

@RogerW
Oh, I see. It did not realise it was just a split line.
I always use the backslash character (\) to split a command onto the next line and this does not seem to have to be in the middle of brackets. I wondered if it was a split line but I discounted it because I was looking for the backslash character.
(you learn something everyday!)

Return to “Python”