Apr-18-2021, 10:45 AM
(This post was last modified: Apr-18-2021, 10:46 AM by knoxvilles_joker.)
I started the process of redoing the code so I can place it into the splash screen script I have but I had to redo the initialization part of the script.
class TkApp(tk.Tk): # these arguments initialize the class and how to form arguments within def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)And I am getting this error.
Error:H:\>python selectmenu3.py
Traceback (most recent call last):
File "H:\selectmenu3.py", line 203, in <module>
tk_app = TkApp()
File "H:\selectmenu3.py", line 121, in __init__
start_with_first_selected(False)
NameError: name 'start_with_first_selected' is not defined
I am trying to read up on the collections man pages and examples I can find and see if there is a different nomenclature I need to use for islice and deque to make it work properly as I believe it is a similar issue to the buttons having to be called as tk.Button instead of just Button due to the way the script pieces are initiated.from collections import deque from itertools import islice from tkinter import * import tkinter import tkinter.ttk as ttk import tkinter as tk import time from threading import Thread GUN = "" class TkApp(tk.Tk): # these arguments initialize the class and how to form arguments within def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Adjust size self.geometry("640x400") self.configure(bg='black') self.gun = tk.StringVar() self.gun.set(GUN) # Specify Grid tk.Grid.rowconfigure(self, 0, weight=1, uniform='a') tk.Grid.rowconfigure(self, 1, weight=1, uniform='b') tk.Grid.rowconfigure(self, 2, weight=1, uniform='b') tk.Grid.rowconfigure(self, 3, weight=1, uniform='b') tk.Grid.rowconfigure(self, 4, weight=1, uniform='b') tk.Grid.rowconfigure(self, 5, weight=1, uniform='b') tk.Grid.rowconfigure(self, 6, weight=1, uniform='b') tk.Grid.rowconfigure(self, 7, weight=1, uniform='b') tk.Grid.rowconfigure(self, 8, weight=1, uniform='b') tk.Grid.rowconfigure(self, 9, weight=1, uniform='b') tk.Grid.columnconfigure(self, 0, weight=1, uniform='d') tk.Grid.columnconfigure(self, 1, weight=1, uniform='d') tk.Grid.columnconfigure(self, 2, weight=1, uniform='d') tk.Grid.columnconfigure(self, 3, weight=1, uniform='d') tk.Grid.columnconfigure(self, 4, weight=1, uniform='d') tk.Grid.columnconfigure(self, 5, weight=1, uniform='d') tk.Grid.columnconfigure(self, 6, weight=1, uniform='d') tk.Grid.columnconfigure(self, 7, weight=1, uniform='d') button_1 = tk.Button(self, justify=tk.CENTER, text="AUTO-REMOTE", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') button_2 = tk.Button(self, justify=tk.CENTER, text="HARD", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') button_3 = tk.Button(self, justify=tk.CENTER, text="SEMIHARD", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') button_4 = tk.Button(self, justify=tk.CENTER, text="SOFT", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') sbutton_1 = tk.Button(self, justify=tk.CENTER, text="BIO", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') sbutton_2 = tk.Button(self, justify=tk.CENTER, text="INERT", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') tsbutton_1 = tk.Button(self, justify=tk.CENTER, text="MULTI SPEC", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') tsbutton_2 = tk.Button(self, justify=tk.CENTER, text="INFRA RED", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') tsbutton_3 = tk.Button(self, justify=tk.CENTER, text="UV", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') wsbutton_1 = tk.Button(self, justify=tk.CENTER, text="SAFE", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') wsbutton_2 = tk.Button(self, justify=tk.CENTER, text="ARMED", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') isbutton_1 = tk.Button(self, justify=tk.CENTER, text="SEARCH", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') isbutton_2 = tk.Button(self, justify=tk.CENTER, text="TEST", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') isbutton_3 = tk.Button(self, justify=tk.CENTER, text="ENGAGED", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') isbutton_4 = tk.Button(self, justify=tk.CENTER, text="INTERROGATE", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') trbutton_1 = tk.Button(self, justify=tk.CENTER, text="AUTO", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') trbutton_2 = tk.Button(self, justify=tk.CENTER, text="SELECTIVE", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') smman = tk.Button(self, justify=tk.CENTER, text="MAN-OVERRIDE", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') smauto = tk.Button(self, justify=tk.CENTER, text="SEMI-AUTO", bg='black', fg='yellow',highlightbackground='yellow', highlightcolor='black', activebackground='yellow') # displays gun id on top left and right of screen gun_1 = tk.Button(self, borderwidth=7,bg='black',fg='yellow',disabledforeground='yellow',state=tk.DISABLED,textvariable=self.gun, highlightcolor = 'yellow') gun_2 = tk.Button(self, borderwidth=7,bg='black',fg='yellow',disabledforeground='yellow',state=tk.DISABLED,textvariable=self.gun) headernew = tk.Button(self, bg='black', state=tk.DISABLED, fg='yellow', disabledforeground='yellow',highlightbackground='yellow', borderwidth=2, justify=CENTER, text="UA 571-C \n REMOTE SENTRY WEAPON SYSTEM") syst = tk.Button(self, bg='black', state=tk.DISABLED, fg='yellow', disabledforeground='yellow',highlightbackground='yellow', borderwidth=2, justify=CENTER, text="SYSTEM \n MODE") weap = tk.Button(self, bg='black', state=tk.DISABLED, fg='yellow', disabledforeground='yellow',highlightbackground='yellow', borderwidth=2, justify=CENTER, text="WEAPON \n STATUS") iff = tk.Button(self, bg='black', state=tk.DISABLED, fg='yellow', disabledforeground='yellow',highlightbackground='yellow', borderwidth=2, justify=CENTER, text="IFF \n STATUS") test = tk.Button(self, bg='black', state=tk.DISABLED, fg='yellow', disabledforeground='yellow',highlightbackground='yellow', borderwidth=2, justify=CENTER, text="TEST \n ROUTINE") target = tk.Button(self, bg='black', state=tk.DISABLED, fg='yellow', disabledforeground='yellow',highlightbackground='yellow', borderwidth=2, justify=CENTER, text="TARGET PROFILE") spectral = tk.Button(self, bg='black', state=tk.DISABLED, fg='yellow', disabledforeground='yellow',highlightbackground='yellow', borderwidth=2, justify=CENTER, text="SPECTRAL PROFILE") ts = tk.Button(self, bg='black', state=tk.DISABLED, fg='yellow', disabledforeground='yellow',highlightbackground='yellow', borderwidth=2, justify=CENTER, text="TARGET SELECT") gun_1.grid(row=0, column=0, ipadx=2, ipady=2, padx=2, pady=2, sticky=W) gun_2.grid(row=0, column=7, ipadx=2, ipady=2, padx=2, pady=2, sticky=E) headernew.grid(row=0, column=1, columnspan=6, sticky="NSEW") button_1.grid(row=2, column=0, columnspan=2, sticky="NSEW") button_2.grid(row=9, column=0, columnspan=3, sticky="NSEW") button_3.grid(row=8, column=0, columnspan=3, sticky="NSEW") button_4.grid(row=7, column=0, columnspan=3, sticky="NSEW") sbutton_1.grid(row=7, column=3, columnspan=2, sticky="NSEW") sbutton_2.grid(row=8, column=3, columnspan=2, sticky="NSEW") tsbutton_1.grid(row=7, column=5, columnspan=3, sticky="NSEW") tsbutton_2.grid(row=8, column=5, columnspan=3, sticky="NSEW") tsbutton_3.grid(row=9, column=5, columnspan=3, sticky="NSEW") wsbutton_1.grid(row=2, column=2, columnspan=2, sticky="NSEW") wsbutton_2.grid(row=3, column=2, columnspan=2, sticky="NSEW") isbutton_1.grid(row=2, column=4, columnspan=2, sticky="NSEW") isbutton_2.grid(row=3, column=4, columnspan=2, sticky="NSEW") isbutton_3.grid(row=4, column=4, columnspan=2, sticky="NSEW") isbutton_4.grid(row=5, column=4, columnspan=2, sticky="NSEW") trbutton_1.grid(row=2, column=6, columnspan=2, sticky="NSEW") trbutton_2.grid(row=3, column=6, columnspan=2, sticky="NSEW") smman.grid(row=3, column=0, columnspan=2, sticky="NSEW") smauto.grid(row=4, column=0, columnspan=2, sticky="NSEW") syst.grid(row=1, column=0, columnspan=2, sticky="NSEW") weap.grid(row=1, column=2, columnspan=2, sticky="NSEW") iff.grid(row=1, column=4, columnspan=2, sticky="NSEW") test.grid(row=1, column=6, columnspan=2, sticky="NSEW") target.grid(row=6, column=0, columnspan=3, sticky="NSEW") spectral.grid(row=6, column=3, columnspan=2, sticky="NSEW") ts.grid(row=6, column=5, columnspan=3, sticky="NSEW") # this quits by clicking a button labeled q quit = tk.Button(self, bg='black', fg='yellow', text='q', command=self.quit) quit.grid(row=8,column=0,sticky="SW") self.bind('<KeyPress-Down>', self.on_down) self.bind("<Up>", self.on_up) self.bind("<Left>", self.on_left) self.bind("<Right>", self.on_right) self.bind("<KeyPress-a>", self.on_keypress_a) self.bind("<KeyPress-b>", self.on_keypress_b) self.bind("<KeyPress-c>", self.on_keypress_c) self.bind("<KeyPress-d>", self.on_keypress_d) self.bind("<KeyPress-q>", self.on_keypress_q) group1 = deque((button_1, smman, smauto)) group2 = deque((wsbutton_1, wsbutton_2)) group3 = deque((isbutton_1, isbutton_2, isbutton_3, isbutton_4)) group4 = deque((trbutton_1, trbutton_2)) group5 = deque((button_4, button_3, button_2)) group6 = deque((sbutton_1, sbutton_2)) group7 = deque((tsbutton_1, tsbutton_2, tsbutton_3)) header_group = deque(((syst, group1), (weap, group2),(iff, group3), (test, group4),(target, group5), (spectral, group6),(ts, group7))) start_with_first_selected(False) # toggle_header_group(header_group) def toggle_button_group(button_group): button_group[0].config(foreground='black', background='yellow', relief='sunken') for button in islice(button_group, 1, None): button.config(foreground='yellow', background='black', relief='raised') def toggle_header_group(header_group): header_group[0][0].config(disabledforeground='black', background='yellow', relief='sunken') for button in islice(header_group, 1, None): button[0].config(disabledforeground='yellow',background='black', relief='raised') def start_with_first_selected(first_selected=False): if first_selected: toggle_button_group(group1) toggle_button_group(group2) toggle_button_group(group3) toggle_button_group(group4) toggle_button_group(group5) toggle_button_group(group6) toggle_button_group(group7) else: # set last of each group to selected group1.rotate(1) group2.rotate(1) group3.rotate(1) group4.rotate(1) group5.rotate(1) group6.rotate(1) group7.rotate(1) # start_with_first_selected(False) # toggle_header_group(header_group) def on_down(self, evet): group = header_group[0][1] group.rotate(-1) toggle_button_group(group) def on_up(self, evet): group = header_group[0][1] group.rotate(1) toggle_button_group(group) def on_left(self, evet): header_group.rotate(1) toggle_header_group(header_group) def on_right(self, evet): header_group.rotate(-1) toggle_header_group(header_group) # these work def on_keypress_a(self, evet): which_gun = self.gun.get() which_gun = "A" self.gun.set(which_gun) def on_keypress_b(self, evet): which_gun = self.gun.get() which_gun = "B" self.gun.set(which_gun) def on_keypress_c(self, evet): which_gun = self.gun.get() which_gun = "C" self.gun.set(which_gun) def on_keypress_d(self, evet): which_gun = self.gun.get() which_gun = "D" self.gun.set(which_gun) # this reloads the window, you have to click it to make it the active window again def on_keypress_r(self, evet): self.destroy() self.__init__() # this quits and closes the window by hitting a single key def on_keypress_q(self, evet): self.quit() # this executes the main loop and tkinter self delcared class. It declares the function then runs it in the main loop. tk_app = TkApp() tk_app.mainloop()