代码不根据从输入框中获取的内容执行数值计算
Code not performing numerical calculation from what it is obtaining from the entry box
我正在学习 tkinter GUI 开发,目前正在尝试构建一个通过手动输入汇率来转换货币的应用程序。目前,当我按下程序 GUI 上的转换按钮时,我遇到了以下错误。
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "c:\Akshit\Currency converter\main.py", line 17, in conversion_logic
cvalue_tbci = float(cvalue_tbc) * 1.3930973
ValueError: could not convert string to float: ''
这是我的代码,它有一些我计划用于选择特定货币的其他输入框,一旦我的代码启动并且运行,现在它被注释掉了。
from tkinter import *
from tkinter.ttk import *
#window sizing and background
root= Tk()
root.title("Currency Converter")
root.state("zoomed")
#styles
style = ttk.Style()
style.configure("TEntry", foreground="black", background="#ffffff")
#functions for the logic
def conversion_logic():
cvalue_tbci = float(cvalue_tbc) * 1.3930973
currency_tbci_display_box = Label(root, borderwidth=3, relief="sunken", width=85, anchor="center", text=cvalue_tbci)
currency_tbci_display_box.grid(ipadx=5, ipady=2, row=46, column=10)
#what each widget will do
#currency_tbc_entry = Entry(root, width=73, justify="center", style="TEntry")
#currency_tbc_entry.insert(0, "Please choose currency to be converted: USD, AUD, INR, MYR, GBP, EUR, JPY, CNY")
#currency_tbc = currency_tbc_entry.get()
currency_tbc_value = Entry(root, width=45, justify="center", style="TEntry")
cvalue_tbc = currency_tbc_value.get()
#currency_tbci_entry = Entry(root, width=73, justify="center", style="TEntry")
#currency_tbci_entry.insert(0, "Please choose currency to be converted in: USD, AUD, INR, MYR, GBP, EUR, JPY, CNY")
#currency_tbci = currency_tbci_entry.get()
currency_tbci_display_label = Label(root, borderwidth=2, relief="ridge", text="The value currency to be converted in will display here", anchor="center")
Convert_button = Button(root, text="CONVERT", width=20, command=conversion_logic)
#placing the widgets
#currency_tbc_entry.grid(ipady=5, ipadx=5, row=15, column= 9)
currency_tbc_value.grid(ipadx=5, ipady=5, row=17, column=10)
#currency_tbci_entry.grid(ipady=5, ipadx=5, row=15, column=15)
currency_tbci_display_label.grid(ipadx=20, ipady=5, row=38, column=10)
希望这些信息足以解决我的问题。
提前致谢
由于您在创建 currency_tbc_value
之后调用了 cvalue_tbc = currency_tbc_value.get()
,您将得到空字符串,因为尚未输入任何内容。
您需要在 conversion_logic()
中调用 cvalue_tbc = currency_tbc_value.get()
:
def conversion_logic():
try:
cvalue_tbc = currency_tbc_value.get()
cvalue_tbci = float(cvalue_tbc) * 1.3930973
currency_tbci_display_box = Label(root, borderwidth=3, relief="sunken", width=85, anchor="center", text=cvalue_tbci)
currency_tbci_display_box.grid(ipadx=5, ipady=2, row=46, column=10)
except ValueError:
print(f'Invalid value input: "{cvalue_tbc}"')
请注意,我已使用 try/except
处理无效输入。
我正在学习 tkinter GUI 开发,目前正在尝试构建一个通过手动输入汇率来转换货币的应用程序。目前,当我按下程序 GUI 上的转换按钮时,我遇到了以下错误。
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "c:\Akshit\Currency converter\main.py", line 17, in conversion_logic
cvalue_tbci = float(cvalue_tbc) * 1.3930973
ValueError: could not convert string to float: ''
这是我的代码,它有一些我计划用于选择特定货币的其他输入框,一旦我的代码启动并且运行,现在它被注释掉了。
from tkinter import *
from tkinter.ttk import *
#window sizing and background
root= Tk()
root.title("Currency Converter")
root.state("zoomed")
#styles
style = ttk.Style()
style.configure("TEntry", foreground="black", background="#ffffff")
#functions for the logic
def conversion_logic():
cvalue_tbci = float(cvalue_tbc) * 1.3930973
currency_tbci_display_box = Label(root, borderwidth=3, relief="sunken", width=85, anchor="center", text=cvalue_tbci)
currency_tbci_display_box.grid(ipadx=5, ipady=2, row=46, column=10)
#what each widget will do
#currency_tbc_entry = Entry(root, width=73, justify="center", style="TEntry")
#currency_tbc_entry.insert(0, "Please choose currency to be converted: USD, AUD, INR, MYR, GBP, EUR, JPY, CNY")
#currency_tbc = currency_tbc_entry.get()
currency_tbc_value = Entry(root, width=45, justify="center", style="TEntry")
cvalue_tbc = currency_tbc_value.get()
#currency_tbci_entry = Entry(root, width=73, justify="center", style="TEntry")
#currency_tbci_entry.insert(0, "Please choose currency to be converted in: USD, AUD, INR, MYR, GBP, EUR, JPY, CNY")
#currency_tbci = currency_tbci_entry.get()
currency_tbci_display_label = Label(root, borderwidth=2, relief="ridge", text="The value currency to be converted in will display here", anchor="center")
Convert_button = Button(root, text="CONVERT", width=20, command=conversion_logic)
#placing the widgets
#currency_tbc_entry.grid(ipady=5, ipadx=5, row=15, column= 9)
currency_tbc_value.grid(ipadx=5, ipady=5, row=17, column=10)
#currency_tbci_entry.grid(ipady=5, ipadx=5, row=15, column=15)
currency_tbci_display_label.grid(ipadx=20, ipady=5, row=38, column=10)
希望这些信息足以解决我的问题。
提前致谢
由于您在创建 currency_tbc_value
之后调用了 cvalue_tbc = currency_tbc_value.get()
,您将得到空字符串,因为尚未输入任何内容。
您需要在 conversion_logic()
中调用 cvalue_tbc = currency_tbc_value.get()
:
def conversion_logic():
try:
cvalue_tbc = currency_tbc_value.get()
cvalue_tbci = float(cvalue_tbc) * 1.3930973
currency_tbci_display_box = Label(root, borderwidth=3, relief="sunken", width=85, anchor="center", text=cvalue_tbci)
currency_tbci_display_box.grid(ipadx=5, ipady=2, row=46, column=10)
except ValueError:
print(f'Invalid value input: "{cvalue_tbc}"')
请注意,我已使用 try/except
处理无效输入。