Python

On this page, i will share python codes that you might find useful. In the first example, you will find an small application to ping a website. I called this app “PyPing” but you can rename it if you wish to do so.

*If anyone wants to share some python codes, please feel free to contact me. I will credit your shares on this page.


PyPing

*Python 3.5

import tkinter as tk
import subprocess


def ping():
    cmd = ["ping", entry.get(), "-c", "2"]
    output = subprocess.check_output(cmd)

    # output = subprocess.check_output("ping {} -c 2".format(entry.get()), shell=True)

    print('>', output)
    # put result in label
    result['text'] = output.decode('utf-8')

my_gui = tk.Tk()
entry = tk.StringVar()

my_gui.geometry('700x400')
my_gui.title("PyPing")

tk.Label(my_gui, text="Enter target IP or host as required.").pack(pady=20)
tk.Entry(my_gui, textvariable=entry).pack(pady=10)
tk.Button(my_gui,text="Ping Test", command=ping).pack(pady=10)

# label for ping result
result = tk.Label(my_gui)
result.pack()

my_gui.mainloop()
PyPing


Here’s a screen shot with a ping result. The result is printed on a label. You can ping another website and the new result will replace the previous label output. You may want to add things like a menu, but for me, i did not see the need for it since it’s such a small application. GitHub and Stack Overflow are great websites to find good ideas and tutorials to create all kinds of applications. I visit them on a regular basis.

~ teklordz


ColorPicker

import sys
if sys.version[0] == '2':                   # for Python 3.X or 2.X
    from Tkinter import *
    from tkColorChooser import askcolor
elif sys.version[0] == '3':
    from tkinter import *
    from tkinter.colorchooser import askcolor

def setBgColor():
    (triple, hexstr) = askcolor()
    if hexstr:
        print(hexstr)
        push.config(bg=hexstr)
        show.set("#RRGGBB string = '" + hexstr + "'")

root = Tk()
root.title('ColorPicker')
root.geometry('500x400')
root.resizable(height=FALSE, width=FALSE)
push = Button(root, text='Press to Select Color', command=setBgColor)
push.config(height=3, font=('times', 20, 'bold'))
push.pack(expand=YES, fill=BOTH)

show = StringVar()
show.set('(hex string appears here)')
Entry(root, textvariable=show,
      justify=CENTER, font='bold').pack(fill=X)

root.mainloop()
ColorPicker

Inspiration for this application was taken from the book “Programming Python, 4th Edition”.  Here’s a screenshot.

 


Scroll Up