切换选项的反转按钮tkin中的菜单值

2024-04-27 03:32:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个货币转换器,它要求用户使用两个optionmenu小部件选择起始货币和结束货币。我遇到的问题是,在货币被转换后,我想创建一个按钮来反转选项菜单值以转换回原始货币。例如,我最初将20美元兑换成欧元。我想按钮,以扭转它转换为20欧元美元,并反映在选项菜单的变化。以下是我的代码:

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')

Tags: selfconfigvaluebodycolumncurrencytklist
1条回答
网友
1楼 · 发布于 2024-04-27 03:32:06

您当前对reverse()的尝试创建了全新的OptionMenus,而且从未实际显示它们。实际上,您不需要触摸OptionMenus本身,只需交换与之相关的两个变量中的值:

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

相关问题 更多 >