hoping someone can help me. I have the working code below that creates a UI for looking up the name of an AD user and Looking up a users AD Groups. It all works fine as IS.
import tkinter
from tkinter import *
from tkinter import messagebox
from tkinter.scrolledtext import ScrolledText
import subprocess
import requests
import urllib3
from requests.auth import HTTPBasicAuth
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
#Get Username
def button_get_user():
USERID = info.get()
name = subprocess.check_output(
'net user' + " " + USERID + " " + '/domain | FIND /I "Full Name"', shell=True
)
full_name = name.replace(b"Full Name", b"").strip()
print_out.delete("1.0", "end")
print_out.insert("end-1c",full_name.decode())
#Get Group Info
def button_get_group():
USERID = info.get()
group = subprocess.check_output(
'dsquery user -name' + " " + USERID + " " + '| dsget user -memberof | dsget group -samid', shell=True
)
print_out.delete("1.0", "end")
print_out.insert("end-1c",group.decode())
#GUI SECTION
#WINDOW PARAMETERS
root = Tk()
root.title("Steve's Network Tool")
#root.iconbitmap('icon.ico')
root.geometry("550x550")
root.resizable(False, False)
#WINDOW MENU OPTIONS
def about():
messagebox.showinfo('Version History', 'Version 1.0 Test Edition')
menubar = Menu(root)
file = Menu(menubar, tearoff=0)
file.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=file)
help = Menu(menubar, tearoff=0)
help.add_command(label="About", command=about)
menubar.add_cascade(label="Help", menu=help)
#INPUT FIELDS - PASSWORD CHARACTERS REPLACED WITH '*'
#LEFT FRAME
frame0 = Frame(root)
frame0.grid(row=1)
tkinter.Label(frame0, text="Enter Input:").grid(row=1)
info = tkinter.Entry(frame0)
info.grid(row=1, column=1)
root_label = tkinter.Label(frame0, text="AD Tasks:", pady=20)
root_label.grid(row=1, column=3)
#GET USERNAME INFO
button_get_info = Button(frame0, text="Username", command=button_get_user)
button_get_info.grid(row=1, column=4, padx=0)
#GET GROUP INFO
button_get_info = Button(frame0, text="Group List", command=button_get_group)
button_get_info.grid(row=1, column=5, padx=0)
#DOMAIN CHOICE
var1 = IntVar()
euprod_checkbutton = Checkbutton(frame0, text="Domain1", variable=var1)
euprod_checkbutton.grid(row=1, column=6, sticky=W)
var2 = IntVar()
prod_checkbutton = Checkbutton(frame0, text="Domain2", variable=var2)
prod_checkbutton.grid(row=1, column=7, sticky=W)
#ACTION BUTTONS & OUTPUT WINDOW
#FRAME3
frame3 = Frame(root)
frame3.grid(row=9, pady=10)
#OUTPUT WINDOW
print_out = ScrolledText(frame3, height=20, width=50, bg="Black", fg='yellow')
print_out.grid(row=3, column=1)
#Copy Text Button
# triggered off left button click on text_field
def copy_text_to_clipboard():
#root.clipboard_clear() # clear clipboard contents
root.clipboard_append(print_out.get("end-1c", info)) # append new value to clipboard
#COPY TO CLIPBOARD BUTTON
button_copy = Button(frame3, text="Copy To Clipboard", command=copy_text_to_clipboard)
button_copy.grid(row=11, column=1)
#CLEAR OUTPUT WINDOW
def clear():
print_out.delete("1.0","end")
button_copy = Button(frame3,text="Clear Output", command=clear)
button_copy.grid(row=12, column=1)
#EXIT PROGRAM BUTTON
button_quit = Button(frame3, text="Exit", command=root.quit, padx=20)
button_quit.grid(row=13, column=1)
root.config(menu=menubar)
root.mainloop()
The check boxes currently are in active. What I would like to do is manipulate this section:
#Get Group Info
def button_get_group():
USERID = info.get()
group = subprocess.check_output(
'dsquery user -name' + " " + USERID + " " + '| dsget user -memberof | dsget group -samid', shell=True
)
If Domain 1 is ticked change
'dsquery user -name' + " " + USERID + " " + '| dsget user -memberof | dsget group -samid'
to
'dsquery user -name -d Domain1' + " " + USERID + " " + '| dsget user -memberof | dsget group -samid'
If Domain 2 is ticked change
'dsquery user -name' + " " + USERID + " " + '| dsget user -memberof | dsget group -samid'
to
'dsquery user -name -d Domain2' + " " + USERID + " " + '| dsget user -memberof | dsget group -samid'
Hope this makes sense. Many thanks in advance.
In situation where you have two or more mutually exclusive options (i.e., only one thing can be selected at a time) it's better to use a group of radio buttons instead of check boxes.
Here's an example
import tkinter as tk
from tkinter import ttk
#Get Group Info
def button_get_group():
USERID = info.get()
domain = radio_var.get()
# PRO TIP: f-strings are your friend here!
group = subprocess.check_output(
f'dsquery user -name {USERID}{domain} | dsget user -memberof | dsget group -samid',
shell=True
)
root = tk.Tk()
# create a variable to store the selection, give it a default value of '' (empty string)
radio_var = tk.StringVar(root, '')
option_none = ttk.RadioButton(
root,
text='None', # text shown on the GUI
value='', # the value this option has
variable=radio_var, # bind the StringVar
)
option_none.pack()
option_one = ttk.RadioButton(
root,
text='Domain1', # text shown on the GUI
value=' -d Domain1', # the value this option has
variable=radio_var, # bind the StringVar
)
option_one.pack()
option_two = ttk.RadioButton(
root,
text='Domain2', # text shown on the GUI
value=' -d Domain2', # the value this option has
variable=radio_var, # bind the StringVar
)
option_two.pack()
... # code omitted for brevity
root.mainloop()