简体   繁体   中英

Python - Functions & Execution order

I want to use the bot_create function with a button but I keep getting (on line 20) the problem "bots not defined" so I moved the function down below the button but got the problem "bot_create not defined". I didn't get this problem using C++ and I'm new to Python. How should I arrange the functions?

import tkinter as tk
import numpy as np
import multiprocessing as mp


bots_max = 1000 # Maximum number of bots
bot = []
bot_count = 0

# Menu functions
def save_field():
    pass

# Field functions
def field_clear():
    pass

# Bots functions
def bots_create():
    bot[bot_count] = bots
    bot_count += 1

main = tk.Tk()

field_sides = 600
ctrls_width = 200
main.geometry("800x600")
main.resizable(0, 0)
main.title("Swarm Simulator v1.0")



# Controls menu on left side
button1 = tk.Button(main, text = "Button 1").pack(side = "left", command = bots_create())



class environment:

    def __init__():
        pass

    class wall:
        def __init__():
            pass

# Bots
class bots:

    alive = True

    def __init__():
        alive = True



# Field where bots live
field = tk.Canvas(main, width = field_sides, height = field_sides, bg = "white").pack(side = "right")
for particle in bots:
    print("|")

main.mainloop()

Here's a version of your code that fixes all the syntactic problems, and so compiles (what I really mean is that my IDE now thinks its ok). It also runs, but I don't know if it does what you intended. See my comments in the code:

import tkinter as tk
import numpy as np
import multiprocessing as mp


# moved your class defs up to fix problems with accessing them before they are defined

class environment:

    def __init__(self):   # need a self param here
        pass

    class wall:
        def __init__(self):   # need a self param here
            pass

# Bots
class bots:

    alive = True

    def __init__(self):   # need a self param here
        alive = True

bots_max = 1000 # Maximum number of bots
bot = []
# bot_count = 0  # this no longer does anything.  use `len(bot)` to get the number of objects in the 'bot' list

# Menu functions
def save_field():
    pass

# Field functions
def field_clear():
    pass

# Bots functions
def bots_create():
#    bot[bot_count] = bots   # this will crash as it is referring to a non-existent location in the list
                             # also, your use of "bots" here makes no sense
#    bot_count += 1          # this makes 'bot_count' a local variable, which is not what you want
    bot.append(bots())       # not sure this is what you want, but this creates a new 'bots' object and adds it to the 'bot' list

main = tk.Tk()

field_sides = 600
ctrls_width = 200
main.geometry("800x600")
main.resizable(0, 0)
main.title("Swarm Simulator v1.0")



# Controls menu on left side
button1 = tk.Button(main, text = "Button 1").pack(side = "left", command = bots_create())



# Field where bots live
field = tk.Canvas(main, width = field_sides, height = field_sides, bg = "white").pack(side = "right")
for particle in bot:   # maybe you want to iterate over the 'bot' list instead of the 'bots' type?
    print("|")

main.mainloop()

As @khelwood says, it seems that you should swap the use of the names bot and bots per the way you are using them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM