不能从循环导入中导入其他类



我正在尝试制作一款关于成为总统的游戏,我有一些卡片可以让你做些什么。我需要让卡片做一些事情,所以我刷新屏幕并改变全局变量,但它抛出了这个:

Traceback (most recent call last):
File "C:UsersFobPycharmProjectsPresidentmain.py", line 1, in <module>
import cards
File "C:UsersFobPycharmProjectsPresidentcards.py", line 7, in <module>
import main
File "C:UsersFobPycharmProjectsPresidentmain.py", line 34, in <module>
refresh()
File "C:UsersFobPycharmProjectsPresidentmain.py", line 30, in refresh
cards.create_cards()
AttributeError: partially initialized module 'cards' has no attribute 'create_cards' (most 
likely due to a circular import)

当我从卡片中删除主导入时,它将工作。我试过调试器,但我试过的那些都不起作用。下面是主类的代码:

from globals import *
import menus
import cards
def back_cmd():
clear()
refresh()
def clear():
for ele in globals.root.winfo_children():
print(ele)
ele.destroy()
def version_log():
clear()
version_list = Label(text = f"Current version: {globals.version}n"
f"Added card playing system, and also squaresn"
f"at the bottom of each card showing how it effectsn"
f"your stats. Added statistics.", font = ("Arial", 20))
back_button = Button(text = "Back", width = 25, height= 5, bg = 'Gray', command=back_cmd)
version_list.pack()
back_button.pack()
def refresh():
root.geometry("640x640")
root.title('Cool')
menus.get_home()
cards.create_cards()
version_button = Button(text="Version 0.01", command=version_log)
version_button.pack()
refresh()
root.mainloop()

下面是cards类的代码:

import globals
import random
from tkinter import *
from PIL import Image, ImageTk
import menus
import main


def generate_card():
rand_int = random.randint(0, 10)
if(rand_int == 1):
return ["Declare tacos as a sandwich", "121"]
elif(rand_int == 2):
return ["Rename french friesn to freedom fries", "211"]
elif(rand_int == 3):
return ["Make all shoes velcro", "312"]
elif(rand_int == 4):
return ["Everyone has to nwear sunglasses", "321"]
elif(rand_int == 5):
return ["Ban pepper throughout nthe country", "132"]
elif(rand_int == 6):
return ["Stop barbie doll production", "312"]
elif(rand_int == 7):
return ["Give everyone free paper", "312"]
elif(rand_int == 8):
return ["Give a speech", "222"]
else:
return ["Make all backpacks blue", "121"]

def deal_effect(s):
s = str(s)
count = 0
for x in s:
if(x == "1"):
if(count == 0):
globals.rep -= (2 + random.randint(1, 3))
count += 1

elif(count == 1):
globals.fed_budg -= (20000000000 + random.randint(1000000000, 7000000000))
count += 1

elif(count == 2):
globals.global_opinion -= (2 + random.randint(1, 5))
count += 1

if (x == "2"):
if (count == 0):
globals.rep -= (1 + random.randint(1, 3))
count += 1

elif (count == 1):
globals.fed_budg -= (4000000000 + random.randint(5000000, 1500000000))
count += 1

elif (count == 2):
globals.global_opinion -= (1 + random.randint(0,1))
count += 1

if (x == "3"):
if (count == 0):
globals.rep += (1 + random.randint(1, 3))
count += 1

elif (count == 1):
globals.fed_budg += (4000000000 + random.randint(50000000, 1500000000))
count += 1

elif (count == 2):
globals.global_opinion += (1 + random.randint(0, 2))
count += 1
main.refresh()

def get_color(arg):
arg = int(arg)
if(arg == 0):
return 'red'
elif(arg == 1):
return 'red'
elif(arg == 2):
return 'orange'
elif(arg == 3):
return 'green'


def create_cards():
card_one_status = generate_card()
card_two_status = generate_card()
# print(card_one_info[0])

print("reached")
card_one = Button(globals.root, text = card_one_status[0], fg = "Black", bg = "White"
, height = 20, width=25, command=deal_effect(card_one_status[1]))
card_two = Button(globals.root, text = card_two_status[0], fg = "Black", bg = "White"
, height = 20, width=25, command=deal_effect(card_two_status[1]))
if(globals.forecast == True):
one = Canvas(globals.root, bg='white', height=20, width=25)
two = Canvas(globals.root, bg='white', height=20, width=25)
three = Canvas(globals.root, bg='white', height=20, width=25)
one.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][0]),
outline=get_color(card_one_status[1][0]))
two.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][1]),
outline=get_color(card_one_status[1][1]))
three.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][2]),
outline=get_color(card_one_status[1][2]))
one.place(x=70, y=350)
two.place(x=140, y=350)
three.place(x=210, y=350)
one = Canvas(globals.root, bg='white', height=20, width=25)
two = Canvas(globals.root, bg='white', height=20, width=25)
three = Canvas(globals.root, bg='white', height=20, width=25)
one.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][0]),
outline=get_color(card_two_status[1][0]))
two.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][1]),
outline=get_color(card_two_status[1][1]))
three.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][2]),
outline=get_color(card_two_status[1][2]))

print(card_one_status[1][2])
one.place(x=407, y=350)
two.place(x=479, y=350)
three.place(x=549, y=350)

card_one.place(x = 60, y = 70)
card_two.place(x=400, y = 70)
import random
from tkinter import *
from PIL import Image, ImageTk
import menus
import main


def generate_card():
rand_int = random.randint(0, 10)
if(rand_int == 1):
return ["Declare tacos as a sandwich", "121"]
elif(rand_int == 2):
return ["Rename french friesn to freedom fries", "211"]
elif(rand_int == 3):
return ["Make all shoes velcro", "312"]
elif(rand_int == 4):
return ["Everyone has to nwear sunglasses", "321"]
elif(rand_int == 5):
return ["Ban pepper throughout nthe country", "132"]
elif(rand_int == 6):
return ["Stop barbie doll production", "312"]
elif(rand_int == 7):
return ["Give everyone free paper", "312"]
elif(rand_int == 8):
return ["Give a speech", "222"]
else:
return ["Make all backpacks blue", "121"]

def deal_effect(s):
s = str(s)
count = 0
for x in s:
if(x == "1"):
if(count == 0):
globals.rep -= (2 + random.randint(1, 3))
count += 1

elif(count == 1):
globals.fed_budg -= (20000000000 + random.randint(1000000000, 7000000000))
count += 1

elif(count == 2):
globals.global_opinion -= (2 + random.randint(1, 5))
count += 1

if (x == "2"):
if (count == 0):
globals.rep -= (1 + random.randint(1, 3))
count += 1

elif (count == 1):
globals.fed_budg -= (4000000000 + random.randint(5000000, 1500000000))
count += 1

elif (count == 2):
globals.global_opinion -= (1 + random.randint(0,1))
count += 1

if (x == "3"):
if (count == 0):
globals.rep += (1 + random.randint(1, 3))
count += 1

elif (count == 1):
globals.fed_budg += (4000000000 + random.randint(50000000, 1500000000))
count += 1

elif (count == 2):
globals.global_opinion += (1 + random.randint(0, 2))
count += 1
main.refresh()

def get_color(arg):
arg = int(arg)
if(arg == 0):
return 'red'
elif(arg == 1):
return 'red'
elif(arg == 2):
return 'orange'
elif(arg == 3):
return 'green'


def create_cards():
card_one_status = generate_card()
card_two_status = generate_card()
# print(card_one_info[0])

print("reached")
card_one = Button(globals.root, text = card_one_status[0], fg = "Black", bg = "White"
, height = 20, width=25, command=deal_effect(card_one_status[1]))
card_two = Button(globals.root, text = card_two_status[0], fg = "Black", bg = "White"
, height = 20, width=25, command=deal_effect(card_two_status[1]))
if(globals.forecast == True):
one = Canvas(globals.root, bg='white', height=20, width=25)
two = Canvas(globals.root, bg='white', height=20, width=25)
three = Canvas(globals.root, bg='white', height=20, width=25)
one.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][0]),
outline=get_color(card_one_status[1][0]))
two.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][1]),
outline=get_color(card_one_status[1][1]))
three.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][2]),
outline=get_color(card_one_status[1][2]))
one.place(x=70, y=350)
two.place(x=140, y=350)
three.place(x=210, y=350)
one = Canvas(globals.root, bg='white', height=20, width=25)
two = Canvas(globals.root, bg='white', height=20, width=25)
three = Canvas(globals.root, bg='white', height=20, width=25)
one.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][0]),
outline=get_color(card_two_status[1][0]))
two.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][1]),
outline=get_color(card_two_status[1][1]))
three.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][2]),
outline=get_color(card_two_status[1][2]))

print(card_one_status[1][2])
one.place(x=407, y=350)
two.place(x=479, y=350)
three.place(x=549, y=350)

card_one.place(x = 60, y = 70)
card_two.place(x=400, y = 70)

有人知道为什么吗?很抱歉时间太长了谢谢!

来自simre的注释

模块(.py文件)中没有类,只有方法。你可以把它们写在同一个文件里,这样就可以了。但它仍然是一种应该(不一定,但很可能)重新设计的代码气味…

最新更新