我正在创建一个可视化不同排序算法的程序。在一个模块中,我有一个名为Algorithms的类,该类将我所有的排序和搜索算法都作为自己的方法保存,而我有另一个模块称为main,它包含其他与程序有关的内容,例如变量和函数。主要问题是我需要将算法类导入主模块,并将变量和函数导入算法模块。但是我收到了ImprotError
主模块
from algorithms import Algorithm
from tkinter import *
import random
import time
a_list = [] # values of the list I want to sort, starts empty
bar_coords = [] # list of lists of coordinates for each of the bars represented in the canvas
list_of_numbers = [] # to be used by the algorithm class
def initialize_list_elements(num_of_elements, up_to):
global list_of_numbers
clear_lists()
for _ in range(num_of_elements):
list_of_numbers.append(random.randint(1, up_to))
return list_of_numbers
def random_colour_code():
hex_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
colour_code = '#'
for i in range(0,6):
colour_code = colour_code + choice(hex_chars)
return colour_code
def clear_lists():
global a_list
global list_of_numbers
list_of_numbers.clear()
a_list.clear()
canvas.delete('all')
def number_list():
global list_of_numbers
return list_of_numbers
def draw():
x = 0
global a_list
a_list = initialize_list_elements(30, 600) # same as the
for value in a_list: # so for each value in our list of values
x = x + 30 # each
y = 700 - int(value) # assign a point y
bars = canvas.create_line((x, 700, x, y), width = 10, fill='black') # then we draw a line using the points (x, 200) and (x, y)
canvas.create_text(x, y-10, text=str(value))
bar_coords = canvas.coords(bars)
algorithm_list = ['Binary Search', 'Jump Search', 'Linear Search', 'Bubble Sort', 'Heap Sort', 'Insertion Sort', 'Merge Sort', 'Quick Sort', 'Selection Sort']
window = Tk()
window.title('Algorithm Testbed')
title_frame = Frame(window) # a frame used to display the title of the program
button_frame = Frame(window) # a frame to hold the buttons
canvas_frame = Frame(window) # a frame for the canvas
var = StringVar(button_frame)
var.set(algorithm_list[0])
canvas = Canvas(canvas_frame, width=910, height=700, background='white')
title_label = Label(title_frame, text='Searching & Sorting Algorithm Testbed', font='Helvetica 20 bold')
init_button = Button(button_frame, text=' Initialise ', width=15, pady=10, command=draw)
start_button = Button(button_frame, text=' Start ', width=15, pady=10)
pause_button = Button(button_frame, text=' Pause ', width=15, pady=10)
step_button = Button(button_frame, text=' Step ', width=15, pady=10)
clear_button = Button(button_frame, text=' Clear ', width=15, pady=10, command=clear_lists)
drop_down_menu = OptionMenu(button_frame, var, *algorithm_list)
# putting the label onto the title_frame
title_label.grid(rowspan=2, column=0)
# putting buttons onto the button_frame
init_button.grid(rowspan=1, column=0, pady=10, padx=5)
start_button.grid(rowspan=1, column=0, pady=10, padx=5)
pause_button.grid(row=2, column=0, pady=10, padx=5)
step_button.grid(row=3, column=0, pady=10, padx=5)
clear_button.grid(row=4, column=0, pady=10, padx=5)
drop_down_menu.grid(row=5, column=0, pady=10, padx=5)
# putting canvas onto canvas_frame
canvas.grid(row=0, column=0)
# putting frames on the window
title_frame.grid(row=0, columnspan=2)
button_frame.grid(row=1, column=0)
canvas_frame.grid(row=1, column=1)
window.mainloop()
“算法”模块(太长而无法包含,因此仅是简化版本
from main import number_list
from main import canvas
# file contains all the algorithms to be animated.
class Algorithm:
def sort_list(self, list): # Uses library function to sort list
list.sort()
return list
使用上述from和import语句,出现错误:
ImportError:无法从'main'导入名称'number_list'(C:\ Users \ Kirome \ Documents \ GitHub \ algorithm-animation-python \ main.py)
我在算法模块上导入时遇到了相同的错误,在主模块中,from algorithms import Algorithm
语句上也遇到了相同的错误
如果在其中一个模块中只有from / import语句之一,它将运行,但我不知道发生了什么。