我目前工作的一個簡單的俄勒岡小道克隆了一個學校項目。我想用Tkinter的這場比賽,因爲目前的版本只Pygame的上升到Python 2和我習慣使用Python 3.我分開了遊戲的不同部分爲獨立的PY文件(main.py,names.py ,occupations.py,store.py,game.py)並將它們全部放入一個文件夾中,以便更有條理。我已經輸入了歡迎界面的代碼和main.py的代碼,names.py,並且爲occupations.py添加了一些代碼。當我運行我的代碼時,會出現名稱'名稱'未定義的錯誤。在名稱文件中,我創建了一個名爲names()的函數,並將其在主文件中運行。我的代碼有什麼問題?NameError:名字的名字「沒有定義
# main.py
from tkinter import *
from names import *
from occupations import *
from store import *
from game import *
root = Tk()
root.resizable(width=True, height=True)
root.wm_title('Oregon Trail')
root.geometry("200x75")
canvas = Canvas(root,width=400,height=200)
canvas.pack()
welcome = Label(canvas,
font = ('System',14,'bold'),
text = 'Oregon Trail')
welcome.grid(row=0,sticky=(N,S,E,W))
play = None
def playbuttoncommands():
canvas.pack_forget()
play.grid_forget()
welcome.grid_forget()
names()
play = Button(canvas,
font = 'System',
text = 'Travel the Trail',
command = playbuttoncommands)
play.grid(row=1,sticky=(N,S,E,W))
root.mainloop()
# names.py
from tkinter import *
from occupations import *
from store import *
from game import *
from main import *
def names():
canvas2 = Canvas(root,width=400,height=200)
canvas.pack()
root.geometry("400x200")
namelabel = Label(root,
font = 'System',
text = 'Please enter the names of your travel group:')
namelabel.grid(row=1,sticky=N)
name1 = ''
name1entry = Entry(root,textvariable=name1)
name1entry.grid(row=2,sticky=N)
name2 = ''
name2entry = Entry(root,textvariable=name2)
name2entry.grid(row=3, sticky=N)
name3 = ''
name3entry = Entry(root,textvariable=name3)
name3entry.grid(row=4,sticky=N)
name4 = ''
name4entry = Entry(root,textvariable=name4)
name4entry.grid(row=5,sticky=N)
name5 = ''
name5entry = Entry(root,textvariable=name5)
name5entry.grid(row=6,sticky=N)
enternames = Button(text = 'Continue', command = submitnames)
enternames.grid(row=7,sticky=N)
# occupations.py
from tkinter import *
from names import *
from store import *
from game import *
from main import *
def submitnames():
name1 = name1entry.get()
name2 = name2entry.get()
name3 = name3entry.get()
name4 = name4entry.get()
name5 = name5entry.get()
# Full Error Report
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
return self.func(*args)
File ***************/main.py", line 33, in playbuttoncommands
names()
NameError: name 'names' is not defined