我目前在我的python类中遇到了我的货币转换程序问题。我试图将一个金额,即条目小部件,从起始货币,第一个选项菜单转换为所需的货币,第二个选项菜单。我花了几个小时阅读文档,所以我希望你能给我一些意见!目前我的列表中只有两种货币,因此更容易测试。所需的结果将是一个动态程序,一旦点击转换按钮,它将能够读取金额,货币1和货币2。这是我的代码:
getAllCouses
答案 0 :(得分:0)
我的回答并不能让我满意,所以我解决了你的问题。 变化:
向currAmount1
,currAmount2
添加了自我关键字,并在脚本中的任何位置反映了此更改。 (currAmount1
- > self.currAmount1
)
在.get()
开头的Tkinter变量上调用curr_search()
:
cur1 = self.curr_start1.get()
cur2 = self.curr_start2.get()
amount = self.currAmount1.get()
我没有将整个web_obj文件打开成一个变量(它不起作用),而是逐行读取文件并在每一行上调用reg表达式搜索。当搜索产生我们期望的结果(除None
之外的任何东西)时,我将此结果存储在final_str中。
在文件末尾添加了一个检查:如果map
返回一个整数值(例如,将1USD转换为USD将返回1),我只获取地图的第一个元素,或者我使用你现有的表达方式。
最后,我实现了一种显示结果的方法:
self.currEntry2.delete(0, tk.END)
self.currEntry2.insert(tk.END, final)
将在第二个输入字段中显示结果。
这是最终的代码:
# Module imports
import Tkinter as tk
import tkMessageBox
import urllib2
import re
# Function Definitions
class Application(tk.Frame):
def __init__(self, parent = None):
tk.Frame.__init__(self, parent)
self.parent = parent
self.setupUI()
self.createWidgets()
def setupUI(self):
self.parent.title("User Input")
self.grid()
self.centerWindow()
def centerWindow(self):
app_width = 307
app_height = 350
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - app_width)/2
y = (sh - app_height)/2
self.parent.geometry('%dx%d+%d+%d' % (app_width, app_height, x, y))
def createWidgets(self):
self.emptyFrame = tk.Frame(self.parent,bg="white")
self.emptyFrame.grid(row=0,column=0,sticky="news")
self.parent.grid_columnconfigure(0,weight=1)
self.parent.grid_rowconfigure(0,weight=1)
self.label1 = tk.Label(self.emptyFrame,text="Currency Converter",bg="white",font="Arial 26")
self.label1.grid(row=0,column=0, sticky="news")
self.label2 = tk.Label(self.emptyFrame,text="Visit http://www.xe.com/iso4217.php\nFor Currency Code Information",bg="white",font="Arial 8")
self.label2.grid(row=0,column=0, sticky="news")
self.label2.place(x = 55, y = 40)
self.currAmount1 = tk.StringVar() # Adding user imput for what value they want to convert
self.currEntry1 = tk.Entry(self.parent, textvariable=self.currAmount1,bd=2,width=28,background="white")
self.currEntry1.grid()
self.currEntry1.place(x = 27, y = 125)
self.currAmount2 = tk.StringVar()
self.currEntry2 = tk.Entry(self.parent,textvariable=self.currAmount2,bd=2,width=28,background="white")
self.currEntry2.grid()
self.currEntry2.place(x = 27, y = 195)
self.currency_list1 = ['USD','GBP'] #Creating the Currency Options menu and adding a list for all the currencies
self.curr_start1 = tk.StringVar()
self.curr_start1.set(self.currency_list1[0])
self.currencyMenu1 = tk.OptionMenu(self.parent, self.curr_start1, *self.currency_list1)
self.currencyMenu1.grid(row=0,column=0)
self.currencyMenu1.place(x = 230, y = 120)
self.currency_list2 = ['USD','GBP'] #Creating the Currency Options menu and adding a list for all the currencies
self.curr_start2 = tk.StringVar()
self.curr_start2.set(self.currency_list2[0])
self.currencyMenu2 = tk.OptionMenu(self.parent, self.curr_start2, *self.currency_list2)
self.currencyMenu2.grid(row=0,column=0)
self.currencyMenu2.place(x = 230, y = 190)
self.convertBtn = tk.Button(self.parent, text="Convert", font="Times 12", command=self.curr_search)
self.convertBtn.grid()
self.convertBtn.place(x = 120,y = 225)
def curr_search(self):
cur1 = self.curr_start1.get()
cur2 = self.curr_start2.get()
amount = self.currAmount1.get()
url = 'https://www.google.com/finance/converter?'
final_url = url + 'a=' + str(amount) + '&from=' + str(cur1) + '&to=' + str(cur2) # Adds the paramters the user inputs to the general url for converting to currencies
web_obj = urllib2.urlopen(final_url)
#Read each line , check if reg expression is in
for line in web_obj:
line = line.replace('\n', '')
line = re.search('class=bld>(.+?)</span>', line)
if line is not None:
final_str = line
final_str = final_str.group(1) # Extracts the string between the class and </span> which is the final conversion
final_num = map(int, re.findall(r'\d+', final_str)) # Extracts the integers from the final_str so that it wouldn't have the currency code in the final. Puts in a list
#If number is a decimal number, group, else take integer
if len(final_num) > 1:
final = str(final_num[0]) + '.' + str(final_num[1]) # Adds the decimal back from
else:
final = str(final_num[0])
web_obj.close()
self.currEntry2.delete(0, tk.END)
self.currEntry2.insert(tk.END, final)
return final
# Main body
root = tk.Tk()
root.resizable(width=False, height=False)
app = Application(root)
root.mainloop()