簡體   English   中英

反向按鈕可在tkinter中切換選項菜單值

[英]reverse button to switch optionmenu values in tkinter

我正在創建一個貨幣轉換器,要求用戶使用兩個選項菜單小部件選擇開始和結束貨幣。 我遇到的問題是貨幣轉換后,我想創建一個按鈕,該按鈕可以反轉optionmen值以轉換回原始貨幣。 例如,我最初將20 USD轉換為EUR。 我希望該按鈕反轉以將20 EUR轉換為USD並在optionmenus中反映更改。 這是我的代碼:

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

您當前對reverse()嘗試將創建全新的OptionMenus-從未實際顯示它們。 您實際上不需要觸摸OptionMenu本身,只需交換它們所關聯的兩個變量中的值即可:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM