【问题标题】:Tkinter math operation with DB variable带有 DB 变量的 Tkinter 数学运算
【发布时间】:2018-03-09 15:26:06
【问题描述】:

我需要对取自
的变量进行数学运算 a: 一个数据库
b: tkinter 入口

#Get database number
cantidad = IntVar()
cursor.execute('''SELECT terneros FROM animales''')
cantidad.set(cursor.fetchone())

然后,一个 tkinter 条目:

ent_peso = Entry (ventana1, textvariable=peso).grid(row=3, column=2)

当我尝试进行以下操作时

total = StringVar()
total.set(peso*cantidad.get())
messagebox.showinfo("Resultado", "$: "+ total.get())

我明白了

TypeError: getdouble() argument must be str, not tuple

完整代码:

    peso = IntVar()
    def calcular():
        cantidad = IntVar()
        cursor.execute('''SELECT terneros FROM animales''')
        cantidad.set(cursor.fetchone())
        total = StringVar()
        total.set(peso.get()*cantidad.get())
        messagebox.showinfo("Resultado", "$: "+ total.get())




    ent_peso = Entry (ventana, textvariable=peso).grid(row=3, column=2)
    but_calc = Button(ventana, text="Calcular", command=calcular).place(x=150,y=115)

    ventana.mainloop()

【问题讨论】:

  • 转换成字符串
  • @ADWAN “它”是什么意思?
  • 没有您提供的代码片段足以重现您遇到的错误。

标签: python database sqlite tkinter


【解决方案1】:

您的问题在于以下代码行:

cantidad.set(cursor.fetchone())

cursor.fetchone() 方法返回一个包含查询中所有值的元组,在本例中为 (terneros,)。 您应该将这行代码更改为

cantidad.set(cursor.fetchone()[0])

为了得到这个元组的内容。

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    相关资源
    最近更新 更多