当我试图在这段代码中使用turtle方法时,我得到一个错误,它指出“主线程不在主循环中”,这只是整个程序的一个片段,但这是发生错误的地方(不同函数中的所有其他turtle方法都能正常工作):
from threading import Thread
from time import sleep
from pynput import *
from pynput.keyboard import Key, Listener, Controller
from turtle import *
import socket
board = Turtle()
screen = getscreen()
screen.tracer(0, 0)
temp = Turtle()
def textinput(key):
global name
global board
name = []
print(str(key)[1])
if str(key)[1].isalpha() and len(name) < 7: #Check if letter is valid
print("1")
name.append(str(key)[1])
print("2")
board.stamp()
print("3")
board.clear()
print("4")
board.write(''.join(name), align="center", font=("fixedsys", 70)) #Show on screen
print("5")
screen.update()
elif key == Key.enter: #End input
name = ''.join(name)
Listener.stop(listener)
def getroomname(option):
global temp
global board
#Box
temp.fillcolor("White")
temp.width(10)
temp.goto(-150, -60)
temp.down()
temp.begin_fill()
for x in range(2):
temp.forward(300)
temp.left(90)
temp.forward(120)
temp.left(90)
temp.end_fill()
temp.up()
temp.goto(0, 100)
temp.stamp()
screen.update()
#Box
temp.goto(0, -60)
with keyboard.Listener(on_press=textinput) as listener:
listener.join()
getroomname(0)
mainloop()