Table of Contents
- Necessary Libraries
- Imports
- Get System Information Function
- Show System Information Function
- Show Date and Time Function
- Main Block
- Example
- Full Code
Have you ever thought about getting to know your computer a little better? If you’re curious about the nitty-gritty details of your system, such as the operating system or the hardware it’s running on, but want to keep things simple and straightforward, today’s your lucky day.
In today’s article, we are going to create a program that demonstrates system information using the platform
library. Let’s get started!
RELATED:
Necessary Libraries
$ pip install tk
$ pip install datetime
$ pip install platform
First, these are the required libraries that you should install so your code runs properly, you can use the terminal or command prompt:
tkinter
: the tkinter library allows us to create a graphical user interface GUI.
datetime
: this library provides classes for manipulating dates and times.
platform
: This library allows us to access the system’s information.
Although these libraries should be installed by default in Python, so you shouldn’t need to install them, in the unlikely case that they are not, all you have to do is use these commands.
Imports
import platform
from datetime import datetime
import tkinter as tk
from tkinter import ttk
We start by importing platform
, which allows us to access the system’s information, and then we import datetime
so we can work with dates and times, third we import tkinter
which provides us with a graphical user interface (GUI). Lastly, we import ttk
class from tkinter
so that we have access to themed tkinter widgets.
Get System Information Function
Next, we create a function that by using the platform.uname()
method retrieves the system’s information, then turns it into a string and returns it so it can be displayed in our main window.
def get_system_information():
"""
Retrieves and returns system information.
"""
system_info = platform.uname()
info_text = (
f"System: {system_info.system}\n"
f"Node Name: {system_info.node}\n"
f"Release: {system_info.release}\n"
f"Version: {system_info.version}\n"
f"Machine: {system_info.machine}\n"
f"Processor: {system_info.processor}"
)
return info_text
Show System Information Function
def show_system_information():
"""
Displays system information in a Tkinter window.
"""
# Create the main window
window = tk.Tk()
window.title("System Information - The Pycodes")
# Create a label to display system information
1
arrow_right
info_label = ttk.Label(window, wraplength=400, font=("Arial", 12))
info_label.grid(row=0, column=0, padx=20, pady=20)
# Set the label text with system information
info_text = get_system_information()
info_label.config(text=info_text)
# Create a button to show the current date and time
date_time_button = ttk.Button(window, text="Show Date and Time", command=show_date_time)
date_time_button.grid(row=1, column=0, pady=10)
# Run the Tkinter event loop
window.mainloop()
Following that, we define a function that sets a main window giving it a title, and create a widget that displays the system’s information that was retrieved by the previous function. Then, we make a button “Show Date” Once clicked triggers the show_date_time
function.
Show Date and Time Function
def show_date_time():
"""
Displays the current date and time in a new Tkinter window.
"""
# Create a new window for date and time
dt_window = tk.Toplevel()
dt_window.title("Current Date and Time")
# Create a label to display the current date and time
dt_label = ttk.Label(dt_window, text=f"Current Date and Time: {datetime.now()}", font=("Arial", 12))
dt_label.pack(padx=20, pady=20)
This function creates a new window that gives the current date and time.
Main Block
if __name__ == "__main__":
# Call the function to show system information
show_system_information()
Lastly, this part ensures that the script runs smoothly once the user clicks run.
Example
I have run this code on Windows as shown below:
Also on Linux system:
Full Code
import platform
from datetime import datetime
import tkinter as tk
from tkinter import ttk
def get_system_information():
"""
Retrieves and returns system information.
"""
system_info = platform.uname()
info_text = (
f"System: {system_info.system}\n"
f"Node Name: {system_info.node}\n"
f"Release: {system_info.release}\n"
f"Version: {system_info.version}\n"
f"Machine: {system_info.machine}\n"
f"Processor: {system_info.processor}"
)
return info_text
def show_system_information():
"""
Displays system information in a Tkinter window.
"""
# Create the main window
window = tk.Tk()
window.title("System Information - The Pycodes")
# Create a label to display system information
info_label = ttk.Label(window, wraplength=400, font=("Arial", 12))
info_label.grid(row=0, column=0, padx=20, pady=20)
# Set the label text with system information
info_text = get_system_information()
info_label.config(text=info_text)
# Create a button to show the current date and time
date_time_button = ttk.Button(window, text="Show Date and Time", command=show_date_time)
date_time_button.grid(row=1, column=0, pady=10)
# Run the Tkinter event loop
window.mainloop()
def show_date_time():
"""
Displays the current date and time in a new Tkinter window.
"""
# Create a new window for date and time
dt_window = tk.Toplevel()
dt_window.title("Current Date and Time")
# Create a label to display the current date and time
dt_label = ttk.Label(dt_window, text=f"Current Date and Time: {datetime.now()}", font=("Arial", 12))
dt_label.pack(padx=20, pady=20)
if __name__ == "__main__":
# Call the function to show system information
show_system_information()
Happy Coding!