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
#5
(Apr-18-2021, 07:30 PM)Yoriz Wrote: The same applies you have moved a variable into a class and need to be able to access them from another method, add self. to the front of them to turn it into an attribute.
class TkApp(tk.Tk):
   # these arguments initialize the class and how to form arguments within 
   def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)
      ...
      ...
      ...
      self.group1 = deque((button_1, smman, smauto))
      self.group2 = deque((wsbutton_1, wsbutton_2))
      ...
      ...
      ...

   def start_with_first_selected(self, first_selected=False):
      ...
      ...
      ... 
      self.group1.rotate(1)
      self.group2.rotate(1)
      ...
      ...

(Apr-18-2021, 07:08 PM)knoxvilles_joker Wrote: OK so that is what you call the different structures methods instead of functions. Not having a book or class learning and really only having google/duckduckgo kind of hurts sometimes. Thank you for that piece of information!
It's in the python docs tutorial
https://docs.python.org/3/tutorial/class...namespaces Wrote:By the way, I use the word attribute for any name following a dot — for example, in the expression z.real, real is an attribute of the object z. Strictly speaking, references to names in modules are attribute references: in the expression modname.funcname, modname is a module object and funcname is an attribute of it. In this case there happens to be a straightforward mapping between the module’s attributes and the global names defined in the module: they share the same namespace!
https://docs.python.org/3/tutorial/class...ce-objects Wrote:The other kind of instance attribute reference is a method. A method is a function that “belongs to” an object. (In Python, the term method is not unique to class instances: other object types can have methods as well. For example, list objects have methods called append, insert, remove, sort, and so on. However, in the following discussion, we’ll use the term method exclusively to mean methods of class instance objects, unless explicitly stated otherwise.)

Thanks for that. I will have to reread that tutorial a few times to digest it a little more fully. I had to reference self so many times, I think that this code might be a narcissist...

The fixed code thanks to your pointers:
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)
        self.group1 = deque((button_1, smman, smauto))
        self.group2 = deque((wsbutton_1, wsbutton_2))
        self.group3 = deque((isbutton_1, isbutton_2, isbutton_3, isbutton_4))
        self.group4 = deque((trbutton_1, trbutton_2))
        self.group5 = deque((button_4, button_3, button_2))
        self.group6 = deque((sbutton_1, sbutton_2))
        self.group7 = deque((tsbutton_1, tsbutton_2, tsbutton_3))
        self.header_group = deque(((syst, self.group1), (weap, self.group2),(iff, self.group3), (test, self.group4),(target, self.group5), (spectral, self.group6),(ts, self.group7)))
        self.start_with_first_selected(False)
        self.toggle_header_group(self.header_group) 
 
    def toggle_button_group(self, 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(self, header_group):
        header_group[0][0].config(disabledforeground='black', background='yellow', relief='sunken')
        for button in islice(self.header_group, 1, None): 
            button[0].config(disabledforeground='yellow',background='black', relief='raised')
 
 
    def start_with_first_selected(self, first_selected=False):
        if first_selected:
           toggle_button_group(self.group1)
           toggle_button_group(self.group2)
           toggle_button_group(self.group3)
           toggle_button_group(self.group4)
           toggle_button_group(self.group5)
           toggle_button_group(self.group6)
           toggle_button_group(self.group7)
        else:
        # set last of each group to selected
           self.group1.rotate(1)
           self.group2.rotate(1)
           self.group3.rotate(1)
           self.group4.rotate(1)
           self.group5.rotate(1)
           self.group6.rotate(1)
           self.group7.rotate(1)
        

 
    def on_down(self, evet):
       group = self.header_group[0][1]
       group.rotate(-1)
       self.toggle_button_group(group)
  
    def on_up(self, evet):
       group = self.header_group[0][1]
       group.rotate(1)
       self.toggle_button_group(group)
 
    def on_left(self, evet):
       self.header_group.rotate(1)
       self.toggle_header_group(self.header_group)
  
    def on_right(self, evet):
       self.header_group.rotate(-1)
       self.toggle_header_group(self.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
RE: [split] NameError: name 'start_with_first_selected' is not defined - by knoxvilles_joker - Apr-18-2021, 07:59 PM
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 279 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,105 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,886 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,320 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,513 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,915 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,344 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,967 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,381 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,887 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