简体   繁体   English

转换 Label 输出(字符串、整数、浮点数)

[英]Converting Label outputs (string,int,float)

Iam relatively new to python and I've been trying to make a "software" with turtle and tkinter libraries, where the goal is to draw a polygon with n sides where n is the entry input in a label. Theres also a simple button which basically initiates the whole process.我对 python 比较陌生,我一直在尝试用 turtle 和 tkinter 库制作一个“软件”,目标是绘制一个有 n 边的多边形,其中 n 是 label 中的输入输入。还有一个简单的按钮基本上启动了整个过程。 I've got to a point where i need to define u as the angle, whereas u=360/n;however i keep getting this error:我已经到了需要将 u 定义为角度的地步,而 u=360/n;但是我不断收到此错误:

invalid literal for int() with base 10: '' or unsupported operand type(s) for /: 'int' and 'StringVar invalid literal for int() with base 10: ''unsupported operand type(s) for /: 'int' and 'StringVar

I've tried converting the output of the label to both float or int types of information, however I can't quite seem to figure it out.我试过将 label 的 output 转换为 float 或 int 类型的信息,但我似乎不太明白。 How should I proceed with converting the output from the entry?我应该如何从条目中转换 output? Any help is much appreciated and I wish anyone reading this a pleasant rest of the day.非常感谢任何帮助,我希望阅读这篇文章的任何人今天愉快 rest。 Here is the code so far:这是到目前为止的代码:

import turtle
import random
#################
canv=tkinter.Canvas(width=400,height=400)
canv.pack()
#################
tcanv=turtle.TurtleScreen(canv)
t=turtle.RawTurtle(tcanv)


n=tkinter.StringVar()
label=tkinter.Label(text="Sides?")
label.pack()
entry=tkinter.Entry(textvariable="n")
entry.pack()


u=(360/n) if n != 0 else 0

def draw():
    for i in range (n):
        t.fd(50)
        t.left(u)

btn=tkinter.Button (text="Draw",fg="black",command=Draw)
btn.pack()
label.mainloop()
entry.mainloop()
btn.mainloop()

You have some problems with your usage of tkinter, I suggest you try followingsome guide first.您在使用 tkinter 时遇到一些问题,我建议您先尝试遵循一些指南

For your code, try this:对于您的代码,试试这个:

import turtle
import tkinter as tk

def draw():
    n = int(entry.get())
    u = (360 / n) if n != 0 else 0
    for i in range(n):
        t.fd(50)
        t.left(u)


if __name__ == '__main__':
    root = tk.Tk()

    canv = tk.Canvas(root, width=400, height=400)
    canv.pack()

    tcanv = turtle.TurtleScreen(canv)
    t = turtle.RawTurtle(tcanv)

    label = tk.Label(root, text="Sides?")
    label.pack()

    entry = tk.Entry(root)
    entry.pack()

    btn = tk.Button(text="Draw", fg="black", command=draw)
    btn.pack()

    root.mainloop()

(Only reordered your code and changed the string var) (仅重新排序您的代码并更改了字符串 var)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM