Skip to content

Instantly share code, notes, and snippets.

@asdf913
Last active January 2, 2023 12:07
Show Gist options
  • Save asdf913/57406c695dffe51604c7702dc1a19c0f to your computer and use it in GitHub Desktop.
Save asdf913/57406c695dffe51604c7702dc1a19c0f to your computer and use it in GitHub Desktop.
import tkinter as tk
from tkinter import messagebox
import tkinter.ttk as ttk
import configparser
import subprocess
import os
import sys
#from subprocess import check_output
root=tk.Tk()
root.title("IP Configuration")
#canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas=tk.Canvas(root)
#
#def hello ():
#label1 = tk.Label(root, text= 'Hello World!', fg='blue', font=('helvetica', 12, 'bold'))
# canvas.create_window(150, 200, window=label1)
#button1=tk.Button(text='Click Me', command=hello, bg='brown',fg='white')
#canvas1.create_window(150, 150, window=button1)
#canvas1.create_window()
lf=tk.LabelFrame(text="Profile")
lf.grid(sticky="W",ipadx=10,ipady=5,padx=10,row=1,columnspan=10*2-1)
#
buttons=[]
#
#https://stackoverflow.com/questions/404744/determining-application-path-in-a-python-exe-generated-by-pyinstaller
#
#https://pyinstaller.org/en/stable/runtime-information.html
#
if getattr(sys,"frozen",False):
application_path=os.path.dirname(sys.executable)
elif __file__:
application_path=os.path.dirname(os.path.abspath(__file__))
#
config=configparser.ConfigParser()
#tk.messagebox.showinfo(None,"{}{}{}".format(application_path,os.sep,"configuration.ini"));
config.read("{}{}{}".format(application_path,os.sep,"configuration.ini"))
#
lastProfileButton=None
#
def profileButtonClicked(event):
#
button=event.widget
#if lastProfileButton==button:
# return
#
#lastProfileButton=button
#
textName.delete(1.0,tk.END)
textIpAddress.delete(1.0,tk.END)
textSubnetAddress.delete(1.0,tk.END)
textGatewayAddress.delete(1.0,tk.END)
#
#svSource.set(None)
#isSourceChanged=False
#
if config.has_section(str(button.data)):
#
if config.has_option(str(button.data),"name"):
textName.insert(tk.END,config.get(str(button.data),"name"))
#
st=tk.DISABLED if config.has_option(str(button.data),"source") and config.get(str(button.data),"source")=="dhcp" else tk.NORMAL;
print(st)
#
if config.has_option(str(button.data),"ipAddress"):
textIpAddress.insert(tk.END,config.get(str(button.data),"ipAddress"))
textIpAddress .config(state=st)
#
if config.has_option(str(button.data),"subnetAddress"):
textSubnetAddress.insert(tk.END,config.get(str(button.data),"subnetAddress"))
textSubnetAddress .config(state=st)
#
if config.has_option(str(button.data),"gatewayAddress"):
textGatewayAddress.insert(tk.END,config.get(str(button.data),"gatewayAddress"))
textGatewayAddress.config(state=st)
#
if config.has_option(str(button.data),"source"):
svSource.set(config.get(str(button.data),"source"))
svSource.profileChanged=True
#isSourceChanged=True
#
#if !isSourceChanged then:
#
#
buttonCountRange=range(1,3)
#
for i in buttonCountRange:
#
if i==1 :
s=tk.Label(lf,text=" ")
s.grid(row=1,column=i)
#
#button=tk.Button(lf,command=profileButtonClicked,text="Profile %s" % i)
button=tk.Button(lf,text="Profile %s" % i)
#
button.data=i
#
if config.has_option(str(button.data),"buttonName"):
button.configure(text=config.get(str(button.data),"buttonName"))
#
button.grid(row=1,column=i*2)
#button["command"]=lambda btn=button:profileButtonClicked(btn)
button.bind("<Button-1>", profileButtonClicked);
#button["data"]=iPY_VAR0
buttons.append(button)
#if i<11-1 :
if i<buttonCountRange.stop-1 :
s=tk.Label(lf,text=" ")
s.grid(row=1,column=i*2+1)
#s=ttk.Separator(lf,orient=tk.VERTICAL)
#s.grid(row=1,column=i*2+1)
#
#s=tk.Label(root,text=" ")
#s.grid(row=2,columnspan=2)
#
lf=tk.LabelFrame(text="Configuration")
lf.grid(ipadx=10,ipady=5,padx=10,pady=5,sticky="W",row=3)
#lf.grid(sticky="W",row=1,columnspan=10*2-1)
#
labelName=tk.Label(lf,text="Name")
labelName.grid(row=1,column=1)
#
textName=tk.Text(lf,height=1)
textName.grid(row=1,column=2)
#
labelSource=tk.Label(lf,text="Source")
labelSource.grid(row=2,column=1)
#
def sourceChanged(event):
if svSource.profileChanged==True:
svSource.profileChanged=None
return
#
st=tk.DISABLED if svSource.get()=="dhcp" else tk.NORMAL;
textIpAddress .config(state=st)
textSubnetAddress .config(state=st)
textGatewayAddress.config(state=st)
#
sources=("dhcp","static")
svSource=tk.StringVar(canvas)
svSource.set(None)
omSource=tk.OptionMenu(lf,svSource,*sources)
omSource.bind("<Configure>",sourceChanged)
omSource.grid(row=2,column=2,sticky="W")
#
#root.event_generate("<Configure>",when="tail")
#
#svSource.trace("w",sourceChanged)
#
labelIpAddress=tk.Label(lf,text="IP Address")
labelIpAddress.grid(row=3,column=1)
#
textIpAddress=tk.Text(lf,height=1)
textIpAddress.grid(row=3,column=2)
#
labelSubnetAddress=tk.Label(lf,text="Subnet")
labelSubnetAddress.grid(row=4,column=1)
#
textSubnetAddress=tk.Text(lf,height=1)
textSubnetAddress.grid(row=4,column=2)
#
labelGatewayAddress=tk.Label(lf,text="Gateway")
labelGatewayAddress.grid(row=5,column=1)
#
textGatewayAddress=tk.Text(lf,height=1)
textGatewayAddress.grid(row=5,column=2)
#
def applyButtonClicked():
if tk.messagebox.askyesno("Title","Apply?"):
tk.messagebox.showinfo("Title","Applied")
#check_output("dir C:", shell=True)
#subprocess.call("ver",shell=True)
subprocess.call("netsh interface ipv4 show address \"{}\"".format(textName.get("1.0",tk.END)),shell=True)
#check_output("dir C:1")
#
buttonApply=tk.Button(text="Apply",command=applyButtonClicked)
buttonApply.grid(pady=5,row=4)
#
#canvas.pack()
#
root.mainloop()
#
@asdf913
Copy link
Author

asdf913 commented Jan 2, 2023

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment