我想通过按钮使用bot_create函数,但是(在第20行上)我不断遇到“ bots not defined”的问题,因此我将函数移至按钮下方,但出现了“ bot_create not defined”的问题。 使用C ++并没有遇到这个问题,我是Python的新手。我应该如何安排功能?
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()
答案 0 :(得分:0)
这是您的代码的一个版本,可修复所有语法问题,因此可以进行编译(我的意思是我的IDE现在认为可以)。它也可以运行,但是我不知道它是否达到了您的预期。在代码中查看我的评论:
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()
正如@khelwood所说,似乎您应该按照使用方式来交换名称bot
和bots
的使用
答案 1 :(得分:0)
感谢您的帮助。 我现在遇到了其他一些问题。我正在尝试在单击的地方生成新的机器人,但是我在网上发现的功能并没有太大帮助。
def click(event):
x = event.x
y = event.y
field.bind('<Button-1>', click)
return x, y
class bot:
alive = True
def __init__(self, pos_x, pos_y, bot_type, alive):
alive = True
def Create_single():
# Create single bot on mouse click
pos_x, pos_y = click(event)
bots.append(bot(pos_x, pos_y, bot_type, True))
bots_type_count[int((bot_type)-1)] += 1
我无法使click()函数正常工作。