simple tkinter battery monitor

Dave Angel d at davea.name
Mon Jan 28 00:21:32 CET 2013


On 01/27/2013 05:59 PM, leonix.power at gmail.com wrote:
> I tried to write a simple battery monitor for laptops which shows normally just the battery percentage, and when is clicked some more info.
> If I click just one time it works, but if I click a second time, the additional info Label seems to be empty (but it holds the dimension of the StringVar content, even if it isn't displayed)
> Where am I wrong?

See inline comment.

> ---------------------------------------------------
> #!/usr/bin/python3.2
>
> from re import findall, search
> from threading import Thread
> from time import sleep
> from subprocess import Popen, call, PIPE, STDOUT
> from tkinter import *
>
>
>
> class battery_monitor:
>
>          def __init__(self):
>                  root=Tk()
> #                root.geometry("-0+0")
>                  root.overrideredirect(True)
>                  root.wm_attributes("-topmost", 1)
>                  self.battery_string=StringVar()
>                  self.battery_percent=StringVar()
>                  self.battery_label=Label(root, extvariable=self.battery_string, font=("fixed", 9))

I don't know tkinter very well, and I don't have 3.2, but the keyword 
seems to be wrong.  Don't you want textvariable=  ??


>                  self.battery_icon=Label(root, textvariable=self.battery_percent, font=("fixed", 9), width=3)
>                  self.battery_icon.grid()
>                  t=Thread(target=self.update_battery_level_loop)
>                  t.start()
>                  root.bind("<Button-1>", self.display_details)
>                  self.root=root
>                  root.mainloop()
>
>          # displays a message about details of battery status
>          # i.e. "on-line" or "charging, 20 min left" and so on
>          def display_details(self, event):
>                  self.battery_icon.grid_remove()
>                  self.battery_label.grid()
>                  self.root.update_idletasks()
>                  sleep(1)

Never use sleep() inside a gui's main thread.  There are other ways to 
delay the UI without locking it up.

>                  self.battery_label.grid_remove()
>                  self.battery_icon.grid()
>                  self.root.update_idletasks()
>
>          # dummy function used just to test the GUI
>          def read_battery_level(self):
>                  self.level=100
>                  return "battery is full"
>
>          # threaded function, should constantly update the battery level
>          def update_battery_level_loop(self):
>                  self.read_battery_level()
>                  while True:
>                          self.battery_percent.set(self.level)
>                          self.battery_string.set(self.read_battery_level())
>                          sleep(5)

This sleep() is okay.

>
>
>
>
>
> ##############################################
> #
> # main
>
> battery_monitor()
>


-- 
DaveA



More information about the Python-list mailing list