构建海龟内置文本字段,但语句不起作用 [英] Building a built-in text field for turtle, while statement doesn't work

查看:70
本文介绍了构建海龟内置文本字段,但语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个文本字段模块,该模块有一个问题:为什么Python IDLE在while ... break语句中不起作用?

I have built a text field module that has one problem: why does Python IDLE not work for while...break statements?

最初我没有break语句,但是这没有用,所以我添加了break语句,同样的问题仍然存在.

Originally I didn't have a break statement and this didn't work so I added the break statement and the same problems persisted.

这是一个很长的脚本.您将需要其中的所有内容. 不要在repl.it中运行,因为它不会运行.但是它确实在IDLE上运行.

This is a long script. You will need everything in it. Do not run in repl.it, as it will not run. However it does run on IDLE.

https://repl.it/@SUPERMECHM500/TextField

正如脚本中所注释的那样,第610行的while语句未加载到IDLE上,并且break无法按预期工作:如果TextField.FullOutput对象包含文本,则继续执行代码.

As commented in the script, the while statement at line 610 doesn't load on IDLE, and the break doesn't work as expected: Continue the code if the TextField.FullOutput object has text.

我的所有问题均与repl.it有关,因此请不要在答案中提及. 从python IDLE中以文本文件形式运行和调试此脚本.

None of my problems relate to repl.it so don't mention it in your answers. Run and debug this script as a Text file from python IDLE.

推荐答案

首先,一些小问题:

def listenforkeys(bool):

bool是Python关键字,请选择其他变量名称. (例如flag.)此or不适用于您的想法:

bool is a Python keyword, choose a different variable name. (E.g. flag.) This or doesn't work the way you think:

if TextField.FullOutput and TextField.inp != "" or []:

重读or.请勿将用于TextField利用率的同一乌龟用于该TextField的实现:

Reread about or. Don't use the same turtle for TextField implementation that you use for TextField utilization as this statement:

t.clear()

清除TextField用户使用t完成的所有操作.使用其他乌龟.最后,您对mainloop()的呼叫在错误的位置.调用它后,您的代码将停止,tkinker事件处理程序将接管工作.通常这应该是您的代码设计的最后一件事.

wipes out all the actions done with t by user of TextField. Use a different turtle. Finally, your call to mainloop() is in the wrong place. Once you invoke it, your code stops and the tkinker event handler takes over. It generally should be the last thing your code des.

现在是主要问题,此利用率代码:

Now the main issue, this utilization code:

while TextField.FullOutput == "": #Hope this works with Windows IDLE...
  tm.sleep(0.1)
  print("Waiting for input...")
  if TextField.FullOutput != "":
    print('Data sent to RAM.')
    break

不要循环等待缓冲区填充.这确实应该是一个事件,但至少是一个回调.我在下面重写了您的代码以使其成为回调,请参见Enter()函数和示例用法代码.现在,它在IDLE和命令提示符下对我有用.我还做了很多其他小的更改以尝试对其进行清理-其中一些可能需要进一步测试...

Don't loop waiting on the buffer to fill. This really should be an event, but at least a callback. I've rewritten your code below to make this a callback, see the Enter() function and example usage code. It now works for me in IDLE and at the command prompt. I've also made lots of other little changes to try to clean it up a bit -- some of these may need further testing...

# Text field that can be used universally.
# Created by SUPERMECHM500 @ repl.it
# Full functionallity can only be achieved by using IDLE on Windows.

from turtle import Screen, Turtle, mainloop

class TextField:
    TextFieldBorderColor = '#0019fc'
    TextFieldBGColor = '#000000'
    TextFieldTextColor = '#ffffff'

    ShiftedDigits = {'1':'!', '2':'@', '3':'#', '4':'$', '5':'%', '6':'^', '7':'&', '8':'*', '9':'(', '0':')'}

    def __init__(self, callBack=None):
        self.callBack = callBack
        self.turtle = Turtle(visible=False)
        self.turtle.speed('fastest')
        self.inp = []
        self.FullOutput = ""
        self.TextSeparation = 7
        self.s = self.TextSeparation
        self.key_shiftL = False

    def DrawTextField(self, Title):
        t = self.turtle
        t.pensize(1)
        t.color(TextField.TextFieldBorderColor)
        t.pu()
        t.goto(-200, -190)
        t.write(Title)
        t.goto(-200, -200)
        t.pd()
        t.goto(200, -200)
        t.goto(200, -250)
        t.goto(-200, -250)
        t.goto(-200, -200)
        t.pu()
        t.goto(-200, -225)
        t.color(TextField.TextFieldBGColor)
        t.pensize(48)
        t.pd()
        t.forward(400)
        t.pu()
        t.goto(-190, -220)
        t.color(TextField.TextFieldTextColor)

    # Defines the function for each defined key.
    def ShiftLON(self):
        print("Capslock toggled.")
        self.key_shiftL = not self.key_shiftL

    def Space(self):
        self.turtle.write(' ')
        self.inp.append(' ')
        self.turtle.forward(self.s)

    def Backspace(self):
        try:
            self.inp.pop(-1)
        except IndexError:
            print("Cannot backspace!")
        else:
            t = self.turtle
            t.pensize(15)
            t.color(TextField.TextFieldBGColor)
            t.forward(10)
            t.backward(self.TextSeparation)
            t.shape('square')
            t.pd()
            t.turtlesize(1.3)  # <<< Doesn't work on repl.it
            t.stamp()
            t.pu()
            t.color(TextField.TextFieldTextColor)
            t.shape('classic')
            t.backward(10)

    def Period(self):
        if self.key_shiftL:
            self.turtle.write('>')
            self.inp.append('>')
            self.turtle.forward(self.s)
        else:
            self.turtle.write('.')
            self.inp.append('.')
            self.turtle.forward(self.s)

    def Comma(self):
        if self.key_shiftL:
            self.turtle.write('<')
            self.inp.append('<')
            self.turtle.forward(self.s)
        else:
            self.turtle.write(',')
            self.inp.append(',')
            self.turtle.forward(self.s)

    def Enter(self):
        if self.inp != []:
            print("Joining input log...")
            self.turtle.pu()
            self.FullOutput = ''.join(self.inp)
            print('joined.')
            print(self.FullOutput)
            print("Output printed.")

            try:
                self.callBack(self.FullOutput)
                print("Data sent to callback.")
            except NameError:
                print("No callback defined.")

            self.turtle.clear()
            print("Display cleared.")

    def digit(self, d):
        if self.key_shiftL:
            d = TextField.ShiftedDigits[d]

        self.turtle.write(d)
        self.inp.append(d)
        self.turtle.forward(self.s)

    def letter(self, abc):
        if self.key_shiftL:
            abc = abc.upper()

        self.turtle.write(abc)
        self.inp.append(abc)
        self.turtle.forward(self.s)

    def listenforkeys(self, flag):  # Whether or not keys are being used for text field.
        s = Screen()

        if not flag:
            for character in 'abcdefghijklmnopqrstuvwxyz':
                s.onkey(None, character)

            for digit in '0123456789':
                s.onkey(None, digit)

            s.onkey(None, 'period')
            s.onkey(None, 'comma')
            s.onkey(None, 'Shift_L')

            # s.onkeyrelease(None, 'Shift_L')

            s.onkey(None, 'space')
            s.onkey(None, 'BackSpace')
            s.onkey(None, 'Return')

            if self.FullOutput or self.inp:
                self.FullOutput = ""  # Reset the text field content
            self.inp = []  # Reset input log

            print("Stopped listening.")

        if flag:
            for character in 'abcdefghijklmnopqrstuvwxyz':
                s.onkey(lambda abc=character: self.letter(abc), character)

            for character in '1234567890':
                s.onkey(lambda d=character: self.digit(d), character)

            s.onkey(self.Period, 'period')
            s.onkey(self.Comma, 'comma')
            s.onkey(self.ShiftLON, 'Shift_L')

            # s.onkeyrelease(self.ShiftLON, 'Shift_L')

            s.onkey(self.Space, 'space')
            s.onkey(self.Backspace, 'BackSpace')
            s.onkey(self.Enter, 'Return')

            s.listen()

            print("Listening.")

if __name__ == "__main__":

    def text_callback(text):
        print("Data received by callback.")
        textField.listenforkeys(False)
        turtle.pu()
        print("Pen up.")
        turtle.write(text, align='center', font=('Arial', 30, 'normal'))
        print("Text written.")

    screen = Screen()
    screen.setup(500, 500)

    textField = TextField(text_callback)
    textField.DrawTextField("Enter Text. Note: Shift is capslock. Capslock disables your keys.")
    print("Text field drawn.")

    textField.listenforkeys(True)
    print("Can type.")

    turtle = Turtle(visible=False)

    mainloop()

这篇关于构建海龟内置文本字段,但语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆