如何修改.textinput中的按钮?

时间:2014-05-01 04:48:29

标签: python button

我试图做一个菜单或类似的东西

以下是代码:

from turtle import TurtleScreen, RawTurtle, TK

root = TK.Tk()
root.title("ArMoon")
canvas = TK.Canvas(root, width=1000, height=700)
canvas.pack()
win = TurtleScreen(canvas)
dali = RawTurtle(win)

def Entrada():
    return win.textinput("Figura"," 1. Operando\n 2. Sumar operandos\
    \n 3. Reiniciar\n 4. Salir\n ")



while True:
    x = Entrada()
    if x == "4" or x == "":
        break
    oper = x.split()
    print(oper)
    x = oper[0]
    print(x)
    #if x <= 0:



win.mainloop()

问题在于,当我运行程序时,在文本输入的小窗口中,它们向我显示两个按钮,我不介意,我喜欢它们但有没有办法修改它们做什么?

&#34; Ok&#34;按钮似乎工作正常,但当我按下&#34;取消&#34;按钮我收到错误

> Traceback (most recent call last):
> 
>   File "C:\Users\Lobo\Desktop\TORTUGA\Borrador v2.py", line 21, in
> <module>
> 
>     oper = x.split()
> 
> AttributeError: 'NoneType' object has no attribute 'split'

我想修改它,所以当我按下它时,只需运行&#39; break&#39;结束。

2 个答案:

答案 0 :(得分:1)

您应该修改您的代码:

x = Entrada()
if x is None or x == "4":
   break

因为取消按钮返回None(类似于C中的空指针)而不是空字符串

答案 1 :(得分:1)

您可以将 textinput 的值存储到变量中

示例:

val = turtle.textinput(title, prompt)
if not val:
   print("blank selected")
elif val == "None"
   print("Cancel Pressed")
else:
   print("do something")