Python Forum
[split] NameError: name 'start_with_first_selected' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] NameError: name 'start_with_first_selected' is not defined
#1
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()
Reply


Messages In This Thread
[split] NameError: name 'start_with_first_selected' is not defined - by knoxvilles_joker - Apr-18-2021, 10:45 AM
RE: arrow key selection for menu - by Yoriz - Apr-18-2021, 10:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 295 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,114 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,906 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,346 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,518 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,930 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,369 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 15,011 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,399 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,896 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020