_tkinter.TclError:预期的整数,但得到“ 1.5”

时间:2019-05-24 16:11:49

标签: python tkinter

您好,我收到一些数字,收到这些数字时相应的框会“点亮”。事情是我试图把盒子放大一些,但是当我编译的时候却给了我这个错误:_tkinter.TclError:预期的整数但是得到了“ 1.5”。

这是我的代码:

# read the TCP sequence received
def sequencia():
    for i in range(0,999):
        s = so.recv(port) + b'\n' #since the sequence received is : 1\n 2\n 5\n etc
        print (s)
        if (len(s)!=0):
            i = int(s)
            labels[i-1].configure(width=12, height=1.5, background="green",foreground="red")
            root.update()
            winsound.PlaySound(sounds[i-1], winsound.SND_FILENAME)
            #time.sleep(1)
            labels[i-1].configure(width=10, height=1, background="gray",foreground="white")
            root.update()          

#window 
root = Tk()
root.title("Sequencia")
#root.attributes('-fullscreen', True)  #for fullscreen
#root.bind('<Escape>',lambda e: root.destroy()) #to get out of the full screen
root.state('zoomed')  #just zoomed not fullscreen
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
fm = Frame(root, width=w, height=h, bg="black")

#draw center lines
#wi = Canvas(fm, width=20, height=20, bg ="green", selectborderwidth=0)
#wi.create_line(0, 200, 150, 150, width=10, fill="blue")



label_sim = Label(fm, width=10, height=1, bg="gray", text = "SIM",  font = ('TkDefaultFont',28), fg="white")
label_nao = Label(fm, width=10, height=1, bg="gray", text = "NÃO", font = ('TkDefaultFont',28), fg="white")
label_fome = Label(fm, width=10, height=1, bg="gray", text = "FOME", font = ('TkDefaultFont',28), fg="white")
label_sede = Label(fm, width=10, height=1, bg="gray", text = "SEDE", font = ('TkDefaultFont',28), fg="white")
label_urinar = Label(fm, width=10, height=1, bg="gray", text = "URINAR", font = ('TkDefaultFont',28), fg="white")
label_ar = Label(fm, width=10, height=1, bg="gray", text = "AR", font = ('TkDefaultFont',28), fg="white")
label_posicao = Label(fm, width=10, height=1, bg="gray", text = "POSIÇÃO", font = ('TkDefaultFont',28), fg="white")



labels = [label_sim,label_nao,label_fome,label_sede,label_urinar,label_ar,label_posicao]

#ordem de sequencia (1 2 3 4 5 6 7): (SIM, NAO, FOME, SEDE, URINAR, AR, POSICAO)
#posicionamento das palavras: 
fm.pack()
label_ar.place(relx=0.5, rely=0.15, anchor=CENTER)
label_posicao.place(relx=0.70, rely=0.3, anchor=CENTER)
label_urinar.place(relx=0.3, rely=0.3, anchor=CENTER)
label_nao.place(relx=0.72, rely=0.55, anchor=CENTER)
label_sim.place(relx=0.28, rely=0.55, anchor=CENTER)
label_sede.place(relx=0.60, rely=0.77, anchor=CENTER)
label_fome.place(relx=0.4, rely=0.77, anchor=CENTER)

root.update()
sequencia()

我编译时的程序给我这个错误和回溯:

Traceback (most recent call last):
  File "C:\Users\meca\Desktop\Python_Exercises\seq_tcp_offline_sem_feedback.py", line 92, in <module>
    sequencia()
  File "C:\Users\meca\Desktop\Python_Exercises\seq_tcp_offline_sem_feedback.py", line 46, in sequencia
    labels[i-1].configure(width=12, height=1.5, background="green",foreground="red")
  File "C:\Users\meca\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1485, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Users\meca\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1476, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: expected integer but got "1.5"

您对如何解决此问题有任何想法吗? 非常感谢

1 个答案:

答案 0 :(得分:1)

此行:
labels[i-1].configure(width=12, height=1.5, background="green",foreground="red")
的高度不能为1.5,它需要一个整数。

如果要大于1,请尝试以下操作:
labels[i-1].configure(width=12, height=2, background="green",foreground="red")