วิธีการเขียน Python ให้แสดงสถานะแบตเตอรี่บนเครื่อง Ubuntu

               Python คือ ชื่อภาษาที่ใช้ในการเขียนโปรแกรมภาษาหนึ่ง ซึ่งถูกพัฒนาขึ้นมาโดยไม่ยึดติดกับแพลตฟอร์ม กล่าวคือสามารถรันภาษา Python ได้ทั้งบนระบบ Unix, Linux , Windows NT, Windows 2000, Windows XP หรือแม้แต่ระบบ FreeBSD อีกอย่างหนึ่งภาษาตัว นี้เป็น OpenSource เหมือนอย่าง PHP ทำให้ทุกคนสามารถที่จะนำ Python มาพัฒนาโปรแกรมของเราได้ฟรีๆโดยไม่ต้องเสียค่าใช้จ่าย และความเป็น Open Source ทำให้มีคนเข้ามาช่วยกันพัฒนาให้ Python มีความสามารถสูงขึ้น และใช้งานได้ครบคุมกับทุกลักษณะงาน สามารถพูดได้เลยว่า Python สามารถทำได้ทุกอย่างก็คงไม่ผิด

มาเข้าประเด็นกันเลยนะครับ วันนี้จะมาแนะนำวิธีการเขียน Python ให้แสดงข้อมูล เปอร์เซ็น แบตเตอร์รี่ โดยใช้งาน Module psutil มาเริ่มกันเลยครับ

ก่อนอื่นในตัวอย่าง จะเป็น Python 3.4 บนระบบปฏิบัติการ Ubuntu นะครับ และใช้ Editer เป็น Pycharm นะครับ

ขั้นตอนที่ 1 ให้เราทำการติดตั้ง มูดูล psutil โดยมีวิธีการดังนี้ 

ให้เปิดตัว terminal ขึ้นมา

pip install psutil

อันนี้เราสามารถติดตั้ง บน Terminal ของ Pycharm เลยได้ 

Terminal ของ Pycharm จะอยู่ที่นี่

ขั้นตอนที่ 2 เมื่อทำการติดตั้งเสร็จแล้วให้ทำตามโค้ดนี้เลยครับ

import psutil

battery_test = psutil.sensors_battery()
percent = int(battery_test.percent)
print("ฺNow battery",percent,"%")

ผลลัพท์

Now battery 75 %

 

เพิ่มเติมถ้าอยากให้มีแบบ notification (นอติฟิเคชั่น) หรือแปลเป็นไทยว่า การแจ้งเตือน แบบว่าให้มันแสดงเป็นแท็บขึ้นมาข้างๆหน้าจอ

ขั้นตอนที่ 1 ให้ทำการติดตั้ง มูดูล plyer

pip install plyer

อันนี้ก็เหมือนกับการติดตั้งมูดูล psutil ได้เลย

 

ขั้นตอนที่ 2 เมื่อทำการติดตั้งเสร็จแล้วให้ทำตามโค้ดนี้เลยครับ

 

import psutil,time
from plyer import battery,notification

battery_test = psutil.sensors_battery()
percent = int(battery_test.percent)
print("ฺNow battery",percent,"%")
percent_text = str(percent)
b = "สถานะแบตเตอรี่ตอนนี้ " + percent_text + "%"
notification.notify(title='ระบบแจ้งเตือนแบตเตอรี่', message=b, app_name='My Battery', app_icon='', timeout=None) # แสดงสถานะแบตเตอรี่
time.sleep(1)

ผลลัพท์

สามารถเรียนรู้บทเรียนเกี่ยวกับ Python เพิ่มเติมได้ที่นี่ บทเรียน Python | VDO สอน Python 

Recent Topics
Subject
Post Reply
Open
Recent
Report A Problem Using The Square Root Calculation Program
By Thanapoom1514 Wed 29 Nov 2023 10:46 am Board English Language
0
1504
Wed 29 Nov 2023 10:46 am By Thanapoom1514 View Topic Report A Problem Using The Square Root Calculation Program
Cyberpanel PHP8.2
By Anonymous Tue 28 Nov 2023 9:44 am Board English Language
1
681
Tue 28 Nov 2023 10:05 am By mindphp View Topic Cyberpanel PHP8.2
Guide to Domain Hosting: Types, Purchasing, and Current Services
By Watkinsayden81 Wed 08 Nov 2023 3:32 pm Board English Language
0
547
Wed 08 Nov 2023 3:32 pm By Watkinsayden81 View Topic Guide to Domain Hosting: Types, Purchasing, and Current Services
Report A Problem Using Program Calculates Tax Withholding
By Thanapoom1514 Thu 26 Oct 2023 11:19 am Board English Language
0
1002
Thu 26 Oct 2023 11:19 am By Thanapoom1514 View Topic Report A Problem Using Program Calculates Tax Withholding
Expert Programming Assignment Help: Your Solution to Coding Challenges
By alfredaharnish Thu 19 Oct 2023 1:33 pm Board English Language
1
560
Sat 04 Nov 2023 7:57 pm By Anonymous View Topic Expert Programming Assignment Help: Your Solution to Coding Challenges
Report A Problem Using The Map Location Search Program
By Thanapoom1514 Thu 14 Sep 2023 4:47 pm Board English Language
1
1468
Tue 19 Sep 2023 4:17 pm By veras View Topic Report A Problem Using The Map Location Search Program
setup microsoft 365
By matthewtaylor39 Tue 05 Sep 2023 3:59 pm Board English Language
1
608
Mon 23 Oct 2023 7:14 pm By veras View Topic setup microsoft 365
Report a problem using Program Exchange Rate
By Thanapoom1514 Wed 21 Jun 2023 4:54 pm Board English Language
0
1975
Wed 21 Jun 2023 4:54 pm By Thanapoom1514 View Topic Report a problem using Program Exchange Rate
Where can I download most recent version of MDUpgrade?
By filipetorres Wed 24 May 2023 9:30 pm Board English Language
3
916
Thu 25 May 2023 4:41 pm By mindphp View Topic Where can I download most recent version of MDUpgrade?
How to plan your E-commerce website
By rachaelwools Mon 08 May 2023 5:48 pm Board English Language
1
752
Mon 15 May 2023 4:42 pm By Reginaler View Topic How to plan your E-commerce website
Report a problem, delete usage data in the domain checker program.
By Thanapoom1514 Mon 17 Apr 2023 4:58 pm Board English Language
0
743
Mon 17 Apr 2023 4:58 pm By Thanapoom1514 View Topic Report a problem, delete usage data in the domain checker program.
Report a problem using the QR code generator
By Thanapoom1514 Mon 03 Apr 2023 11:50 am Board English Language
0
837
Mon 03 Apr 2023 11:50 am By Thanapoom1514 View Topic Report a problem using the QR code generator
Report a problem using the domain checker program
By Thanapoom1514 Thu 30 Mar 2023 12:23 pm Board English Language
0
1776
Thu 30 Mar 2023 12:23 pm By Thanapoom1514 View Topic Report a problem using the domain checker program
Choosing The Perfect Venue for Your Special Day in Phuket
By Justastoryteller Wed 29 Mar 2023 9:35 am Board English Language
1
864
Tue 25 Apr 2023 5:40 pm By davidmikejohn View Topic Choosing The Perfect Venue for Your Special Day in Phuket
How to Decorate Your Apartment in Bangkok on a Budget
By pechevill Tue 28 Mar 2023 2:46 pm Board English Language
1
975
Wed 29 Mar 2023 2:43 pm By Justastoryteller View Topic How to Decorate Your Apartment in Bangkok on a Budget
Report a problem using the EAN-13 barcode generator program
By Thanapoom1514 Tue 28 Mar 2023 11:08 am Board English Language
0
1663
Tue 28 Mar 2023 11:08 am By Thanapoom1514 View Topic Report a problem using the EAN-13 barcode generator program
Report a problem using the program to calculate the air conditioning (BTU)
By Tookta Tue 28 Feb 2023 10:13 am Board English Language
0
2234
Tue 28 Feb 2023 10:13 am By Tookta View Topic Report a problem using the program to calculate the air conditioning (BTU)
Report an issue with the taxi rate calculation program
By flook Tue 21 Feb 2023 11:59 am Board English Language
0
1523
Tue 21 Feb 2023 11:59 am By flook View Topic Report an issue with the taxi rate calculation program
Report a problem using the Program for calculating land price per unit
By Thanapoom1514 Mon 20 Feb 2023 10:54 am Board English Language
0
2828
Mon 20 Feb 2023 10:54 am By Thanapoom1514 View Topic Report a problem using the Program for calculating land price per unit
Report a problem using the total land price calculation program
By Thanapoom1514 Mon 20 Feb 2023 10:24 am Board English Language
0
1400
Mon 20 Feb 2023 10:24 am By Thanapoom1514 View Topic Report a problem using the total land price calculation program