我正试图弄清楚如何查看用户是否从列表框中选择某些货币(现在是EUR'和CAD'),但是当我尝试配置按钮的回调时,它什么都不做。这是我正在尝试的:
if select_option == 1:
global btn_convert
btn_convert.config(command=euro_callback)
elif select_option == 2:
global btn_convert
btn_convert.config(command=cad_callback)
但这不起作用。这是我的清单:
select_option = tk.Listbox(window, height=2, selectmode="SINGLE")
select_option.insert(1, "EURO")
select_option.insert(2, "CAD")
我做错了吗?我不能问用户是否直接在脚本中选择了"EURO"
或"CAD"
选项?
我搜索时发现的是关于常规列表的内容,或者找到许多单独列表的结果(但我想弄清楚的是如何获得该结果并将命令应用于按钮)。当我尝试其中大部分内容时,它什么也没做。
这是完整的代码:
# Imports the tkinter module, which is vital for the GUI
import tkinter as tk
# Imports the html module from the lxml module. We'll be getting our exchange rates from https://www.bloomberg.com
from lxml import html
import requests
# Makes the window
window = tk.Tk()
# Titles the window
window.title("Currency Converter")
# Makes the window 275 x 200 pixels
window.geometry("275x200")
# Makes the background crimson
window.configure(bg="#900C3F")
# window.wm_iconbitmap("penny.ico")
# Gets the information from Bloomberg Markets
page_euro = requests.get('https://www.bloomberg.com/quote/EURUSD:CUR')
page_cad = requests.get('https://www.bloomberg.com/quote/USDCAD:CUR')
tree_euro = html.fromstring(page_euro.content)
tree_cad = html.fromstring(page_cad.content)
'''
When the "Convert" button is pressed, it'll get the value from the text
entry where you put in your value you want to convert to (EUROS or CAD
(Canadian Dollars)), and it'll ask itself; "Is the value all numbers? Or does
it have characters too?". If it doesn't have characters, it'll run as normal.
If it DOES have characters, it'll inform you that, like; "Whoops! You can
only put numbers there!"
'''
def euro_callback():
usd_amount = ent_convert.get()
if str(usd_amount).isdigit():
usd_amount = float(ent_convert.get())
# <div class="price">1.****</div>
euro_exchange = tree_euro.xpath('//div[@class="price"]/text()')
euro_exchange = float(str(euro_exchange[0]))
euro_amount = usd_amount / euro_exchange
lbl_amount.config(text="Euro Amount: %.2f€" % euro_amount)
else:
lbl_amount.config(text="Whoops! You can only put numbers there!")
def cad_callback():
cad_amount = ent_convert.get()
if str(cad_amount).isdigit():
usd_amount = float(ent_convert.get())
# <div class="price">1.2652</div>
cad_exchange = tree.xpath('//div[@class="price"]/text()')
cad_exchange = float(str(cad_exchange[0]))
cad_amount = usd_amount / cad_exchange
lbl_amount.config(text="Canadian Dollar amount: %.2f$" % cad_amount)
else:
lbl_amount.config(text="Whoops! You can only put numbers there!")
btn_convert.config(command=callback)
def callback():
selection = select_option.curselection()[0]
if selection == 1:
# euro_callback()
elif selection == 2:
# cad_callback()
# The list of available currencies to convert to
# lbl_usd = tk.Label(window, text="Enter the USD ($) here:", bg="#900C3F", fg="#FFFFFF")
select_option = tk.Listbox(window, height=2, selectmode="SINGLE")
select_option.insert(1, "EURO")
select_option.insert(2, "CAD")
ent_convert = tk.Entry(window)
# A blank label, followed by a button, which has the usd_callback() command
lbl = tk.Label(window, text=" ", bg="#900C3f")
btn_convert = tk.Button(window, text="Convert", command=callback)
# A blank label, followed by a label that outputs the EURO amount you would get
lbl2 = tk.Label(window, text=" ", bg="#900C3f")
lbl_amount = tk.Label(window, bg="#900C3F", fg="#FFFFFF")
# Packs (adds) all the labels, entries and buttons into the window
select_option.pack()
# lbl_usd.pack()
ent_convert.pack()
lbl.pack()
btn_convert.pack()
lbl2.pack()
lbl_amount.pack()
# Loops the window, and starts the program
window.mainloop()
任何帮助将不胜感激。谢谢!!
我明白了!这是我一直在寻找的答案:
def callback():
selection = select_option.curselection()[0]
if selection == 0:
# euro_callback()
btn_convert.config(command=euro_callback)
elif selection == 1:
# cad_callback()
btn_convert.config(command=cad_callback)
# The list of available currencies to convert to
select_option = tk.Listbox(window, height=2, selectmode="SINGLE")
select_option.insert(0, "EURO")
select_option.insert(1, "CAD")
感谢Novel和Nelson帮助我!!
答案 0 :(得分:4)
问题是当程序启动时,回调被配置一次(理想情况下)。最好的解决方案是使用一个回调来检查列表框,并可以找出要做的事情。
例如:
btn_convert.config(command=callback)
#...
def callback():
selection = select_option.curselection()[0]
if selection == 1:
euro_callback()
elif selection == 2:
cad_callback()