这是我的代码。它在Python 3.5.2中,并使用tkinter和turtle。乌龟窗口出现空白
当按下按钮显示物品的位置时,乌龟窗口是空白的。如果我将所有关于乌龟的代码移到tkinter窗口调用mainloop之后,乌龟窗口在tkinter窗口关闭并且工作正常后打开。
如何修复我的代码,以便在按下按钮时显示。
import csv
import collections
from tkinter import *
def direction(n):
import turtle
wn = turtle.Screen() #sets up window
turtle.bgpic("WilleysMap.gif") #loads picture of map
m = turtle.Turtle() #creates arrow
m.speed(100) #sets the speed
m.penup() #lifts pen so no marks are made
m.forward(160)
m.right(90)
m.forward(80)
m.speed(1) #reduces speed so path can be seen
if n == 1:#------------------------------------------------------------------NU 1
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.right(90)
m.forward(180)
m.right(90)
m.forward(300)
m.left(90)
m.forward(80)
wn.mainloop()
elif n == 2: #---------------------------------------------------------------NU 2
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.right(90)
m.forward(180)
m.right(90)
m.forward(235)
m.left(90)
m.forward(80)
wn.mainloop()
elif n == 3: #---------------------------------------------------------------NU 3
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.right(90)
m.forward(180)
m.right(90)
m.forward(180)
m.left(90)
m.forward(80)
wn.mainloop()
elif n == 4: #---------------------------------------------------------------NU 4
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.right(90)
m.forward(180)
m.right(90)
m.forward(300)
m.right(90)
m.forward(80)
wn.mainloop()
elif n == 5: #---------------------------------------------------------------NU 5
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.right(90)
m.forward(180)
m.right(90)
m.forward(200)
m.right(90)
m.forward(80)
wn.mainloop()
elif n == 6: #---------------------------------------------------------------NU DECK
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.right(90)
m.forward(180)
m.right(90)
m.forward(60)
m.left(90)
m.forward(80)
wn.mainloop()
elif n == 7: #---------------------------------------------------------------PARKING LOT
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.left(90)
m.forward(50)
m.left(45)
m.forward(140)
m.left(45)
m.forward(100)
m.left(45)
m.forward(180)
wn.mainloop()
elif n == 8: #---------------------------------------------------------------HUT AREA
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.right(90)
m.forward(80)
m.right(90)
m.forward(40)
wn.mainloop()
elif n == 9: #---------------------------------------------------------------SIDE STRUCTURE 1
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(20)
m.right(90)
m.forward(180)
m.left(90)
m.forward(30)
m.right(90)
m.forward(80)
wn.mainloop()
else: #----------------------------------------------------------------------SIDE STRUCTURE 2
m.pencolor("red")
m.pensize(15)
m.pendown()
m.forward(80)
m.right(90)
m.forward(60)
wn.mainloop()
with open('code.csv') as f: #loads itemDict from the code.csv file
reader1 = csv.reader(f)
itemDict = dict(reader1)
with open('loc.csv') as f: #loads itemLoc from the loc.csv file
reader2 = csv.reader(f)
itemLoc = dict(reader2)
itemLoc['Emerald Green Arborvitae'] = itemLoc.pop('Emerald Green Arborvitae') #corrects error in translation
global find
def evaluate(event):
if entry.get() in itemDict:
res.configure(text = "UPC: " + str(entry.get()) + "\n The " + itemDict[entry.get()] + " can be found at: " + itemLoc[itemDict[entry.get()]], font = "Verdana 15 bold")
else:
res.configure(text = "UPC: " + str(entry.get()) + "\n The UPC you entered is not listed", font = "Verdana 15 bold")
def show():
if itemLoc[itemDict[entry.get()]] == 'NU 1':
direction(1)
elif itemLoc[itemDict[entry.get()]] == 'NU 2':
direction(2)
elif itemLoc[itemDict[entry.get()]] == 'NU 3':
direction(3)
elif itemLoc[itemDict[entry.get()]] == 'NU 4':
direction(4)
elif itemLoc[itemDict[entry.get()]] == 'NU 5':
direction(5)
elif itemLoc[itemDict[entry.get()]] == 'NU DECK':
direction(6)
elif itemLoc[itemDict[entry.get()]] == 'PARKING LOT':
direction(7)
elif itemLoc[itemDict[entry.get()]] == 'HUT AREA':
direction(8)
elif itemLoc[itemDict[entry.get()]] == 'SIDE STRUCTURE 1':
direction(9)
else:
direction(10)
w = Tk()
w.geometry("650x700")
Label(w, text="Type UPC into entry bar. To see a visual representation of the items location, press the 'Show' button.", font = "Verdana 10").pack(anchor=W)
Label(w, text="").pack()
Label(w, text="Please enter UPC of item", font = "Verdana 10").pack()
Label(w, text="you would like to locate", font = "Verdana 10").pack()
Label(w, text="and press 'Enter'", font = "Verdana 10").pack()
entry = Entry(w)
entry.focus_set()
entry.bind("<Return>", evaluate)
entry.pack()
Label(w, text="").pack()
Button(w, text ="Show", command=show).pack()
Label(w, text="").pack()
res = Label(w)
res.pack()
Label(w, text="Scroll down to see all listed UPCs", font = "Verdana 10").pack()
scrollbar = Scrollbar(w)
scrollbar.pack(side=RIGHT, fill=Y)
T = Text(w, height=15, width=75, bg = 'blue', fg = 'white')
T.pack()
quote = ''
with open('code.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
quote += ("" + row[0] + ' '*(13-len(row[0])) + row[1] + '\n')
T.insert(END, quote)
scrollbar.config(command=T.yview)
w.mainloop()
编辑 问题就出在线路
turtle.bgpic("WilleysMap.gif")
如果我把#在它的前面,代码工作正常,乌龟图形只画在白色屏幕而不是地图。有谁知道如何解决这个错误?
顺便说一下,数据im加载只是一个两个coloured CSV文件。看起来像: Arborvitate,ARB1 金柳,GW34 等 等
编辑 以下是错误我得到
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\lucas\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "x-wingide-python-shell://86210440/2", line 173, in show
File "x-wingide-python-shell://86210440/2", line 29, in direction
File "<string>", line 8, in bgpic
File "C:\Users\lucas\AppData\Local\Programs\Python\Python35-32\lib\turtle.py", line 1482, in bgpic
self._setbgpic(self._bgpic, self._bgpics[picname])
File "C:\Users\lucas\AppData\Local\Programs\Python\Python35-32\lib\turtle.py", line 738, in _setbgpic
self.cv.itemconfig(item, image=image)
File "<string>", line 1, in itemconfig
File "C:\Users\lucas\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2418, in itemconfigure
return self._configure(('itemconfigure', tagOrId), cnf, kw)
File "C:\Users\lucas\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1321, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
如果你在问题补充完整的错误消息,它可以更容易 - 不要求运行的代码。我们无法运行您的代码,因为它需要一些CSV文件和GIF图像。下一次创建一个具有相同问题的小型工作示例 - 然后可能有人会运行它。 – furas
不要在函数之间放置代码 - 很难找到某些东西。 – furas