Search code examples
pythontkinteroptionmenu

reverse button to switch optionmenu values in tkinter


I am creating a currency converter that asks the user to choose the starting and ending currencies with two optionmenu widgets. The problem I have run into is after the currency has been converted, I want to create a button that reverses the optionmenu values to convert back to the original currency. For example, I originally convert 20 USD to EUR. I want the button to reverse it to convert 20 EUR to USD and reflect the change in the optionmenus. Here is the code I have:

currency_list = []
infile = open('currency_list.txt', 'r')
for currency in infile:
    currency_list[:-1]
    currency_list.append(currency.strip("\t\n\t"))
initial1 = currency_list[-16]       # initially set first option menu to USD from currency list
initial2 = currency_list[43]        # initially set second option menu to EUR from currency list
my_list = [currency_list[-16], currency_list[43]]

class CurrencyConverter(tk.Frame):
    def reverse(self):
        my_list.reverse()
        print (my_list)
        self.currency_1_menu = tk.OptionMenu(self, self.currency_1, *currency_list)
        self.currency_2_menu = tk.OptionMenu(self, self.currency_2, *currency_list)

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        body_color = "white"
        body_font_color = "black"
        entry_color = body_color
        entry_font_color = "gray26"

        self.currency_1 = tk.StringVar()        # currency to convert from
        self.currency_2 = tk.StringVar()        # currency to convert to
        self.currency_1.set(my_list[0])         # set value from list above
        self.currency_2.set(my_list[1])         # set value from list above
        self.answer = tk.StringVar()            # result from conversion
        self.value_id = tk.StringVar()          # number user types in to convert from
        self.header_text = tk.StringVar()       # header to display information

        logo = tkinter.PhotoImage(file = "logo_copy.gif")       # import logo. Not working!!
        label = tk.Label(self,image = logo)
        label.config(bg = body_color)
        label.grid(row = 0, column = 2, rowspan = 2)

        self.header = tk.Label(self, textvariable = self.header_text)
        self.result_label = tk.Label(self, textvariable = self.answer, fg = body_font_color)
        self.value_entry = tk.Entry(self, textvariable = self.value_id)
        self.currency_1_menu = tk.OptionMenu(self, self.currency_1, *currency_list)
        self.currency_2_menu = tk.OptionMenu(self, self.currency_2, *currency_list)
        self.calculate_button = tk.Button(self, command = self.convert, text = 'calculate')
        self.reverse_button = tk.Button(self, command = lambda: self.reverse, text = 'reverse')
        home_page_button = tk.Button(self, text = "Home Page", command = lambda: controller.show_frame(StartPage))

        self.header.config(bg = body_color, font = ('arial', 12), fg = entry_font_color)
        self.result_label.config(font = ('Arial', 36))
        self.value_entry.config(font = 'bold', justify = 'center', bg = entry_color, fg = entry_font_color, highlightthickness = 0)
        self.currency_1_menu.config(bg = body_color, width = 25, height = 2)
        self.currency_2_menu.config (bg = body_color, width = 25, height = 2)
        self.result_label.config (bg = body_color)
        self.calculate_button.config (bg = body_color, highlightbackground = body_color)
        self.reverse_button.config (bg = body_color, highlightbackground = body_color)

        self.header.grid(row = 0, column = 0, sticky = 'w')
        self.result_label.grid(row = 1, column = 0, sticky = 'w')
        self.value_entry.grid(row = 2, column = 0)
        self.currency_1_menu.grid (row = 2, column = 1)
        self.currency_2_menu.grid (row = 3, column = 1)
        self.calculate_button.grid(row = 4, column = 0)
        self.reverse_button.grid(row = 2, column = 2, rowspan = 2)
        home_page_button.grid(row = 4, column = 1)

    def convert(self):
        self.currency_1_iso = self.currency_1.get()[0:3]
        self.currency_2_iso = self.currency_2.get()[0:3]

        url = "https://www.xe.com/currencyconverter/convert/?Amount=" + self.value_id.get() + "&From=" + self.currency_1_iso + "&To=" + self.currency_2_iso
        print(url)
        if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
                getattr(ssl, '_create_unverified_context', None)):
            ssl._create_default_https_context = ssl._create_unverified_context
        html_code = urllib.request.urlopen(url).read()
        self.soup = BeautifulSoup(html_code, 'html.parser')
        self.result = self.soup.find('span', {'class': "uccResultAmount"}).string

        self.answer.set(self.result + " " + self.currency_2_iso)
        self.header_text.set(self.value_id.get() + self.currency_1.get()[5:] + ' equals')

Solution

  • Your current attempt at reverse() creates brand new OptionMenus - and never actually displays them. You don't actually need to touch the OptionMenus themselves, just swap the values in the two variables they are tied to:

    temp = self.currency_1.get()
    self.currency_1.set(self.currency_2.get())
    self.currency_2.set(temp)