|
| 1 | +from tkinter import * |
| 2 | +import tkinter as tk |
| 3 | +from tkinter import ttk |
| 4 | +from tkinter.messagebox import showinfo |
| 5 | +from tkinter import scrolledtext |
| 6 | +from settings import * |
| 7 | + |
| 8 | +#Add Menu |
| 9 | + |
| 10 | + |
| 11 | +def showcmdframe(self, choice, *args, **kwargs): |
| 12 | + # this shows the correct frame in the cmd window based on the combo selection. |
| 13 | + print("this is showcmdframe") |
| 14 | + print(f"combobox selected is :{choice}") |
| 15 | + # self.lblUpdate.config(text=f"Using: {connectselect} ") |
| 16 | + ''' |
| 17 | + self.listOfCMDs = ['show version', |
| 18 | + 'show running-config', |
| 19 | + 'show ip interface brief', |
| 20 | + 'show ip route' |
| 21 | + 'show interface gigabitEthernet 0/0', |
| 22 | + 'show interface gigabitEthernet 0/1', |
| 23 | + 'show interface gigabitEthernet 0/2', |
| 24 | + 'show interface gigabitEthernet 0/3', |
| 25 | + 'show interface gigabitEthernet 0/4', |
| 26 | + 'show interface gigabitEthernet 1/0', |
| 27 | + 'show interface gigabitEthernet 1/1', |
| 28 | + 'show arp', |
| 29 | + 'show flash:' |
| 30 | + 'show vlan' |
| 31 | + ] |
| 32 | + ''' |
| 33 | + |
| 34 | + |
| 35 | + ''' |
| 36 | + def populatebox(): |
| 37 | + for i in listOfCMDs: |
| 38 | + self.listBox.insert("end", i) |
| 39 | + ''' |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + if choice == "Paramiko": |
| 44 | + #self.statustext.set("Method: Paramiko") |
| 45 | + #if self.btncopy["state"] == "active" or "normal": self.btncopy["state"] = "disabled" |
| 46 | + #if self.btnpaste["state"] == "active" or "normal": self.btnpaste["state"] = "disabled" |
| 47 | + # self.btnpaste.config(state='disabled') |
| 48 | + |
| 49 | + self.btncopy.configure(state='disabled') |
| 50 | + self.btnpaste.configure(state='disabled') |
| 51 | + |
| 52 | + for widgets in self.cmdframea.winfo_children(): |
| 53 | + widgets.destroy() |
| 54 | + |
| 55 | + self.cmdframecontainer = Frame(self.cmdframea, background="light sky blue") |
| 56 | + self.cmdframecontainer.pack(fill='both', expand=TRUE, anchor='n', side=TOP) |
| 57 | + |
| 58 | + #self.btnsipb=ttk.Button(self.cmdframea, text="Run the selected command", command=self.runcmdselection) |
| 59 | + #self.btnsipb.pack(side='left', anchor='s') |
| 60 | + |
| 61 | + self.listbox = Listbox(self.cmdframecontainer, |
| 62 | + activestyle='none', |
| 63 | + font=("Helvetica", 11, "bold"), |
| 64 | + highlightthickness=0, |
| 65 | + selectforeground='black', |
| 66 | + background='light goldenrod yellow', |
| 67 | + selectbackground="palegreen", |
| 68 | + exportselection=False) # exportselection=False on combobox and listbox to clear conflict |
| 69 | + self.listbox.pack(side = LEFT, fill = 'both', expand=True, anchor='nw', padx=(15, 5), pady=(10, 5)) |
| 70 | + self.scrollbar = Scrollbar(self.cmdframecontainer) |
| 71 | + self.scrollbar.pack(side = RIGHT, fill = BOTH) |
| 72 | + |
| 73 | + ''' |
| 74 | + self.listbox.insert(0, 'show version') |
| 75 | + self.listbox.insert(1, 'show running-config') |
| 76 | + ''' |
| 77 | + # listOfCMDs located in the settigs file |
| 78 | + for i in listOfCMDs: |
| 79 | + self.listbox.insert("end", i) |
| 80 | + |
| 81 | + self.listbox.select_set(1) |
| 82 | + self.cmdvarselected.set("show running-config") |
| 83 | + self.statusvarcmd2.set(value=self.cmdvarselected.get()) |
| 84 | + |
| 85 | + self.listbox.config(yscrollcommand = self.scrollbar.set) |
| 86 | + self.scrollbar.config(command = self.listbox.yview) |
| 87 | + |
| 88 | + def localindex_selected(event): |
| 89 | + selected_indices = self.listbox.curselection() |
| 90 | + selected_index = selected_indices[0] |
| 91 | + selected_value = self.listbox.get(selected_index) |
| 92 | + self.cmdvarselected.set(selected_value) |
| 93 | + print(f"localindex_selected: {selected_value}") |
| 94 | + print(f"localindex_selected cmdvarselected value: {self.cmdvarselected.get()}") |
| 95 | + self.statusvarcmd2.set(value=self.cmdvarselected.get()) # Update status label |
| 96 | + |
| 97 | + self.listbox.bind('<<ListboxSelect>>', localindex_selected) |
| 98 | + |
| 99 | + elif choice == "Netmiko": |
| 100 | + #self.statustext.set("Method: Netmiko") |
| 101 | + self.btncopy.configure(state='disabled') |
| 102 | + self.btnpaste.configure(state='disabled') |
| 103 | + |
| 104 | + for widgets in self.cmdframea.winfo_children(): |
| 105 | + widgets.destroy() |
| 106 | + |
| 107 | + self.cmdframecontainer = Frame(self.cmdframea, background="light sky blue") |
| 108 | + self.cmdframecontainer.pack(fill='both', expand=TRUE, anchor='n', side=TOP) |
| 109 | + |
| 110 | + #self.btnsipb=ttk.Button(self.cmdframea, text="Run the selected command", command=self.runcmdselection) |
| 111 | + #self.btnsipb.pack(side='left', anchor='s') |
| 112 | + |
| 113 | + self.listbox = Listbox(self.cmdframecontainer, |
| 114 | + activestyle='none', |
| 115 | + font=("Helvetica", 11, "bold"), |
| 116 | + highlightthickness=0, |
| 117 | + selectforeground='black', |
| 118 | + background='light goldenrod yellow', |
| 119 | + selectbackground="palegreen", |
| 120 | + exportselection=False) # exportselection=False on combobox and listbox to clear conflict |
| 121 | + self.listbox.pack(side = LEFT, fill = 'both', expand=True, anchor='nw', padx=(15, 5), pady=(10, 5)) |
| 122 | + self.scrollbar = Scrollbar(self.cmdframecontainer) |
| 123 | + self.scrollbar.pack(side = RIGHT, fill = BOTH) |
| 124 | + |
| 125 | + ''' |
| 126 | + self.listbox.insert(0, 'show version') |
| 127 | + self.listbox.insert(1, 'show running-config') |
| 128 | + ''' |
| 129 | + # listOfCMDs located in the settigs file |
| 130 | + for i in listOfCMDs: |
| 131 | + self.listbox.insert("end", i) |
| 132 | + |
| 133 | + self.listbox.select_set(1) |
| 134 | + self.cmdvarselected.set("show running-config") |
| 135 | + self.statusvarcmd2.set(value=self.cmdvarselected.get()) |
| 136 | + |
| 137 | + self.listbox.config(yscrollcommand = self.scrollbar.set) |
| 138 | + self.scrollbar.config(command = self.listbox.yview) |
| 139 | + |
| 140 | + def localindex_selected(event): |
| 141 | + selected_indices = self.listbox.curselection() |
| 142 | + selected_index = selected_indices[0] |
| 143 | + selected_value = self.listbox.get(selected_index) |
| 144 | + self.cmdvarselected.set(selected_value) |
| 145 | + print(f"localindex_selected: {selected_value}") |
| 146 | + print(f"localindex_selected cmdvarselected value: {self.cmdvarselected.get()}") |
| 147 | + self.statusvarcmd2.set(value=self.cmdvarselected.get()) # Update status label |
| 148 | + |
| 149 | + self.listbox.bind('<<ListboxSelect>>', localindex_selected) |
| 150 | + |
| 151 | + #return self.cmdcmdvarselected.get() |
| 152 | + |
| 153 | + elif choice == "Netmiko_ch": |
| 154 | + #self.statustext.set("Method: Netmiko_ch") |
| 155 | + self.btncopy.configure(state='disabled') |
| 156 | + self.btnpaste.configure(state='disabled') |
| 157 | + |
| 158 | + for widgets in self.cmdframea.winfo_children(): |
| 159 | + widgets.destroy() |
| 160 | + |
| 161 | + self.cmdframecontainer = Frame(self.cmdframea, background="light sky blue") |
| 162 | + self.cmdframecontainer.pack(fill='both', expand=TRUE, anchor='n', side=TOP) |
| 163 | + |
| 164 | + #self.btnsipb=ttk.Button(self.cmdframea, text="Run the selected command", command=self.runcmdselection) |
| 165 | + #self.btnsipb.pack(side='left', anchor='s') |
| 166 | + |
| 167 | + self.listbox = Listbox(self.cmdframecontainer, |
| 168 | + activestyle='none', |
| 169 | + font=("Helvetica", 11, "bold"), |
| 170 | + highlightthickness=0, |
| 171 | + selectforeground='black', |
| 172 | + background='light goldenrod yellow', |
| 173 | + selectbackground="palegreen", |
| 174 | + exportselection=False) # exportselection=False on combobox and listbox to clear conflict |
| 175 | + self.listbox.pack(side = LEFT, fill = 'both', expand=True, anchor='nw', padx=(15, 5), pady=(10, 5)) |
| 176 | + self.scrollbar = Scrollbar(self.cmdframecontainer) |
| 177 | + self.scrollbar.pack(side = RIGHT, fill = BOTH) |
| 178 | + |
| 179 | + ''' |
| 180 | + self.listbox.insert(0, 'show version') |
| 181 | + self.listbox.insert(1, 'show running-config') |
| 182 | + ''' |
| 183 | + # listOfCMDs located in the settigs file |
| 184 | + for i in listOfCMDs: |
| 185 | + self.listbox.insert("end", i) |
| 186 | + |
| 187 | + self.listbox.select_set(1) |
| 188 | + self.cmdvarselected.set("show running-config") |
| 189 | + self.statusvarcmd2.set(value=self.cmdvarselected.get()) |
| 190 | + |
| 191 | + self.listbox.config(yscrollcommand = self.scrollbar.set) |
| 192 | + self.scrollbar.config(command = self.listbox.yview) |
| 193 | + |
| 194 | + def localindex_selected(event): |
| 195 | + selected_indices = self.listbox.curselection() |
| 196 | + selected_index = selected_indices[0] |
| 197 | + selected_value = self.listbox.get(selected_index) |
| 198 | + self.cmdvarselected.set(selected_value) |
| 199 | + print(f"localindex_selected: {selected_value}") |
| 200 | + print(f"localindex_selected cmdvarselected value: {self.cmdvarselected.get()}") |
| 201 | + self.statusvarcmd2.set(value=self.cmdvarselected.get()) # Update status label |
| 202 | + |
| 203 | + self.listbox.bind('<<ListboxSelect>>', localindex_selected) |
| 204 | + |
| 205 | + elif choice == "Netmiko_ch_config": |
| 206 | + #self.statustext.set("Method: Netmiko_ch_config") |
| 207 | + self.btncopy.configure(state='normal') |
| 208 | + self.btnpaste.configure(state='normal') |
| 209 | + |
| 210 | + for widgets in self.cmdframea.winfo_children(): |
| 211 | + widgets.destroy() |
| 212 | + |
| 213 | + |
| 214 | + self.cmdframecontainer = Frame(self.cmdframea, background="light sky blue") |
| 215 | + self.cmdframecontainer.pack(fill='both', expand=TRUE, anchor='n', side=TOP) |
| 216 | + |
| 217 | + self.cmdtxtbox = scrolledtext.ScrolledText(self.cmdframecontainer, |
| 218 | + wrap=WORD, |
| 219 | + background='light goldenrod yellow', |
| 220 | + font=("Helvetica", 11, "bold"), |
| 221 | + height=5) #, height=23, width=83 |
| 222 | + self.cmdtxtbox.pack(side = LEFT, fill = 'both', expand=True, anchor='nw', padx=(15, 5), pady=(10, 5)) |
| 223 | + |
| 224 | + # POP UP MENU |
| 225 | + def copy_select(): # copy selected text to clipboard |
| 226 | + global cmddata |
| 227 | + if self.cmdtxtbox.selection_get(): |
| 228 | + cmddata=self.cmdtxtbox.selection_get() # copy selected text to clipboard |
| 229 | + self.clipboard_clear() |
| 230 | + self.clipboard_append(cmddata) |
| 231 | + |
| 232 | + def paste_select(): |
| 233 | + global data |
| 234 | + self.cmdtxtbox.insert(tk.END,self.clipboard_get()) # Paste data from clipboard |
| 235 | + |
| 236 | + self.statusvarcmd2.set(value="Netmiko config multiline") # Update status label |
| 237 | + |
| 238 | + |
| 239 | + #Add Menu |
| 240 | + popup = Menu(self.cmdtxtbox, tearoff=0) |
| 241 | + #Adding Menu Items |
| 242 | + popup.add_command(label="Copy", command=lambda:copy_select()) |
| 243 | + popup.add_command(label="Paste", command=lambda:paste_select()) |
| 244 | + |
| 245 | + def menu_popup(event): |
| 246 | + # display the popup menu |
| 247 | + try: |
| 248 | + popup.tk_popup(event.x_root, event.y_root, 0) |
| 249 | + finally: |
| 250 | + #Release the grab |
| 251 | + popup.grab_release() |
| 252 | + |
| 253 | + self.bind("<Button-3>", menu_popup) |
| 254 | + |
| 255 | + else: |
| 256 | + #self.statustext.set("Method: Undefined") |
| 257 | + self.btncopy.configure(state='disabled') |
| 258 | + self.btnpaste.configure(state='disabled') |
| 259 | + |
| 260 | + for widgets in self.cmdframea.winfo_children(): |
| 261 | + widgets.destroy() |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | + |
| 270 | + |
| 271 | + |
0 commit comments