简体   繁体   English

Python Tkinter:解析时出现意外的EOF

[英]Python tkinter : unexpected EOF while parsing

I got the error unexpected EOF while parsing when i'm trying to create a conversion program with tkinter. 当我尝试使用tkinter创建转换程序时,解析时出现了意外的EOF错误。 Here is my code: 这是我的代码:

from tkinter import *
from tkinter.ttk import *


class conversion:

 def centi():

   def centiMet():
    meStr=a.get()
    meStrc = eval(meStr)
    con=meStrc/100
    conS=str(con)
    CenMet2=Label(root,text=conS + " Meters",font="CenturyGothic 12 bold").pack()
    return

 rootCm= Tk()
 a =  StringVar()
 rootCm.geometry("500x300")
 rootCm.title("Quick Reference Application - Version 0.1.8 [alpha] ")
 label1= Label(rootCm,text="Unit Conversions",font="CenturyGothic 17 bold").pack()
 inputCm= Entry(rootCm,textvariable=a).pack()
 convButton1= Button(rootCm,text="Convert!",command = centiMet).pack()


root= Tk()

root.geometry("500x300")
root.title("Quick Reference Application - Version 0.1.8 [alpha] ")

label1= Label(root,text="Unit Conversions",font="CenturyGothic 17 bold").pack()

CenMet1= Label(root,text="Please select the type you want!!",font="CenturyGothic 12 bold").pack()
convButton1= Button(root,text="Centimeters to Meters",command = conversion.centi).pack()

The error will show after you clicked the centimeter to meter button and try to convert. 单击“厘米到米”按钮并尝试转换后,将显示错误。 It works fine before i tried to make a button to initiate a new window for the cm to meters. 在我尝试创建一个按钮以启动一个新的窗口以显示厘米之前,它工作正常。 Does anyone have any suggestion? 有人有什么建议吗?

As you noticed, the problem seems to be with the second Tk() element. 正如您所注意到的,问题似乎出在第二个Tk()元素上。 I am not 100% sure why this is the case, but it seems to work if you use a Toplevel window instead. 我不是100%知道为什么会这样,但是如果您使用Toplevel窗口代替,它似乎可以工作。

class conversion:
    def centi():
        def centiMet():
            meStr = a.get()
            con = float(meStr) / 100
            Label(rootCm, text="{} Meters".format(con), font="CenturyGothic 12 bold").pack()
        rootCm = Toplevel()
        rootCm.geometry("500x300")
        a =  StringVar()
        Label(rootCm, text="Unit Conversions", font="CenturyGothic 17 bold").pack()
        Entry(rootCm, textvariable=a).pack()
        Button(rootCm, text="Convert!", command=centiMet).pack()

Some more pointers: 一些更多的指针:

  • don't use eval if it can be avoided; 如果可以避免,请不要使用eval use float instead 改用float
  • label = Label(...).pack() will assign the result of pack() to label , not the actual label; label = Label(...).pack()将分配的结果pack()label ,而不是实际的标签; also, you do not seem to need those variables anyway 而且,您似乎也不需要这些变量
  • not sure whether this was intentional or not, but you added the result label to the original root, not to the popup-window 不知道这是否是故意的,但是您将结果标签添加到了原始根目录,而不是添加到了弹出窗口

If you want to reuse the same label when converting multiple measures, use another StringVar : 如果要在转换多个度量时重用同一标签,请使用另一个StringVar

class conversion:
    def centi():
        def centiMet():
            b.set("{} Meters".format(float(a.get()) / 100))
        ...
        b = StringVar()
        Label(rootCm, textvariable=b, font="CenturyGothic 12 bold").pack()

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

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