有人可以告诉我我的代码哪里出错了吗?

huangapple go评论56阅读模式
英文:

Can someone tell me what i did wrong in my code?

问题

这是Python中的KG转换为LBS的计算器代码:

import tkinter as tk
from tkinter import ttk
import ttkbootstrap

# 计算函数
def przelicz():
    kilogram_input = input_string.get()
    funt_output = kilogram_input * 2.204
    output_string.set(funt_output)

# 创建窗口
okno = tk.Tk()
okno.title('Nowe Okno')
okno.geometry('500x250')

# 添加元素
label = ttk.Label(master=okno, text='Kilogramy na Funty', font='Consolas, 40')
label.pack()

input_string = tk.DoubleVar()
input_label = ttk.Entry(okno, textvariable=input_string)
input_label.pack(pady=15)
button = ttk.Button(okno, text='przelicz', style='info', command=przelicz)
button.pack(padx=10)

output_string = tk.DoubleVar()
output = ttk.Label(master=okno, text="", font='Consolas, 30', textvariable=output_string)

# 运行
okno.mainloop()

在计算部分,我更正了错误,将输入和输出的数据类型都更改为DoubleVar,以便进行浮点数运算。

英文:

This is the Code its a KG to LBS calculator in python:

import tkinter as tk 
from tkinter import ttk 
import ttkbootstrap

#funkcja do przeliczania

def przelicz():
    kilogram_input = input_string
    funt_output = output_string
    funt_output = kilogram_input * 2.204
#tworzenie okna 
okno = tk.Tk()
okno.title('Nowe Okno')
okno.geometry('500x250')

#dodawanie rzeczy
label = ttk.Label(master=okno,text = 'Kilogramy na Funty', font='Consolas, 40')
label.pack()

input_string = tk.IntVar()
input_label = ttk.Entry(okno, textvariable=input_string)
input_label.pack(pady=15)
button = ttk.Button(okno, text='przelicz', style='info', command=przelicz)
button.pack(padx=10)

output_string = tk.StringVar()
output = ttk.Label(master=okno, text = "", font='Consolas, 30', textvariable=output_string)

#uruchamianie
okno.mainloop()

SORRY FOR USING POLISH LANGAGUE.

When I try to calculate the mass i see an errors.
funt_output = kilogram_input * 2.204
~~~~~~~~~^
TypeError: unsupported operand type(s) for *: 'IntVar' and 'float'

答案1

得分: 1

您正在尝试将一个 IntVar 对象与一个浮点数(2.204)相乘。在执行乘法运算之前,您需要获取存储在 IntVar 中的值。此外,您需要从 IntVar 更改为 DoubleVar 以接受浮点数。

我尝试了这个,并且它运行得很好。

import tkinter as tk
from tkinter import ttk
import ttkbootstrap

# 将千克转换为磅的函数
def przelicz():
    kilogram_input = input_string.get()
    funt_output = kilogram_input * 2.20462  # 将千克转换为磅
    output_string.set(f"{funt_output:.2f} 磅")

okno = tk.Tk()
okno.title('新窗口')
okno.geometry('500x250')

label = ttk.Label(master=okno, text='千克转磅', font='Consolas, 20')
label.pack()

input_string = tk.DoubleVar()  # 从 IntVar 更改为 DoubleVar 以接受浮点数
input_label = ttk.Entry(okno, textvariable=input_string)
input_label.pack(pady=15)

button = ttk.Button(okno, text='转换', style='info', command=przelicz)
button.pack(padx=10)

output_string = tk.StringVar()
output = ttk.Label(master=okno, textvariable=output_string, font='Consolas, 20')
output.pack(pady=15)

okno.mainloop()

请注意,上述代码已经按照您的要求进行了修改。

英文:

You're trying to multiply an IntVar object with a float number (2.204). You need to retrieve the value stored inside the IntVar before performing the multiplication. Also, you change from IntVar to DoubleVar to accept floating point numbers.

I tried this and it's working well

import tkinter as tk
from tkinter import ttk
import ttkbootstrap

# function to convert kg to lbs
def przelicz():
    kilogram_input = input_string.get() 
    funt_output = kilogram_input * 2.20462  # convert kg to lbs
    output_string.set(f"{funt_output:.2f} lbs")  

okno = tk.Tk()
okno.title('Nowe Okno')
okno.geometry('500x250')

label = ttk.Label(master=okno, text='Kilogramy na Funty', font='Consolas, 20')
label.pack()

input_string = tk.DoubleVar()  # IntVar to DoubleVar to accept floating point numbers
input_label = ttk.Entry(okno, textvariable=input_string)
input_label.pack(pady=15)

button = ttk.Button(okno, text='Przelicz', style='info', command=przelicz)
button.pack(padx=10)

output_string = tk.StringVar()
output = ttk.Label(master=okno, textvariable=output_string, font='Consolas, 20')
output.pack(pady=15)

okno.mainloop()

答案2

得分: 0

因为 input_stringtk.IntVar() 的实例,所以您需要调用 get() 方法来检索存储在变量中的值。

(实际上,input_string 应该是 tk.DoubleVar(),以允许使用浮点(小数)值)

对于 output_string,您会遇到类似的问题,您应该使用 set() 来更新值。

然后,您可以简化函数 przelicz 如下所示:

def przelicz():
    output_string.set(input_string.get() * 2.204)
英文:

Because input_string is an instance of tk.IntVar(), you need to call get() on it in order to retrieve the value stored in the variable

(really, input_string should be a tk.DoubleVar() to allow for floating-poing (decimal) values)

You'll see a similar issue with output_string, where you should use set() to update the value

You can then pare down the function przelicz like so:

def przelicz():
    output_sting.set(input_string.get() * 2.204)

huangapple
  • 本文由 发表于 2023年6月13日 03:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定