I want to use tkinter for exchange conversion.

Asked 2 years ago, Updated 2 years ago, 105 views

I want to do exchange conversion like the image, but it doesn't work well.I don't even know how to insert the image.
Please point it out and make improve it.Enter a description of the image here

import tkinter
import tkinter.messagebox
root=tkinter.Tk()

def convert():


    ifradio_var.get() == 1:# Chinese Yuan
        AusDollar=round (entry_var.get()*0.209234,0)
        tkinter.messagebox.showinfo('Your', str(entry_var), 'Chinese Yuan=', str(AusDollar), 'in Australian Dollars')

    elif currency.upper() == 2:# Euro
        AusDollar=round (entry_var.get()*1.59524,0)
        tkinter.messagebox.showinfo('Your', str(entry_var), 'Euros=', str(AusDollar), 'in Australian Dollars')

    elif currency.upper() == 3:#British Pound
        AusDollar=round (entry_var.get*1.83338,0)
        tkinter.messagebox.showinfo('Your', str(entry_var), 'British Pounds=', str(AusDollar), 'in Australian Dollars')

    elif currency.upper() == 4:#RussianRuble
        AusDollar=round (entry_var.get*0.0214580,0)
        tkinter.messagebox.showinfo('Your', str(entry_var), 'Russian Rubles=', str(AusDollar), 'in Australian Dollars')



main_frame = tkinter.Frame(root).grid()
radio_var=tkinter.IntVar()
radio_var.set(1)
entry_var=tkinter.IntVar()


# create buttons

tkinter.Label(main_frame, text="please select the current to convert").grid(column=1, row=1)

tkinter.Radiobutton(main_frame, text="Chinese Yuan", variable=radio_var,
                    value = 1 ).grid (column = 1, row = 2)
tkinter.Radiobutton(main_frame, text="Euro", variable=radio_var,
                    value=2).grid(column=1, row=3)
tkinter.Radiobutton(main_frame, text="British pound", variable=radio_var,
                    value = 3 ).grid (column = 1, row = 4)
tkinter.Radiobutton(main_frame, text="Russian Rule", variable=radio_var,
                    value = 4 ).grid (column = 1, row=5)
tkinter.Label(main_frame, text="How much money do you want to convert?:") .grid(column=1, row=7)

tkinter.Entry(main_frame, textvariable=entry_var, width=10).grid(column=2, row=7)

tkinter.Button(main_frame,
                                text="Convert", command=convert).grid(column=1, row=8)
quit_button=tkinter.Button(main_frame, text="Quit", command=root.destroy).grid(column=2,row=8)


tkinter.mainloop() 

python tkinter

2022-09-30 19:21

1 Answers

In the code in the question section, there is an error specifying the parameters of tkinter.messagebox.showinfo() and it is an error.

I didn't include the process of inserting the image, but I rewritten it as follows.

from tkinter import Tk, Frame, Label, Button, Radiobutton, Entry, messagebox
from tkinter import IntVar, W

conversion_table = {
  1: {'currency': 'Chinese Yuan', 'rate': 0.209234},
  2: {'currency': 'Euro', 'rate': 1.595240},
  3: {'currency': 'British Pounds', 'rate': 1.833380},
  4: {'currency': 'Russian Rule', 'rate': 0.021458},
}

class CurrencyConverter (Tk):
  def__init__(self):
    Tk.__init__(self)
    self.currency=IntVar(value=1)
    self.money=IntVar(value=0)
    self.counter = 1

    # Widgets
    frame = Frame(self).grid()
    US>Label(
      frame, text = 'Please select the current to convert'
    ) .grid (column=1, row=self.row(), columnspan=4, sticky=W, padx=5)

    # Currents
    fork, item in conversion_table.items():
      Radiobutton(
        frame, text=item['currency'], variable=self.currency, value=k
      ) .grid (column=1, row=self.row(1), columnspan=4, sticky=W, padx=20)

    # Entry box
    US>Label(
      frame, text = 'How much money do you want to convert?'
    ) .grid (column=1, row=self.row(1), columnspan=3, sticky=W, padx=5)
    entry=Entry(frame, textvariable=self.money, width=10)
    entry.grid (column=4, row=self.row(), padx=(1,10))
    entry.configure(
      validate='key', vcmd=(entry.register(
        lambdap —True if p.isdigit() or p==' else False), '%P')
    )

    # Buttons for operations
    Button(
      frame, text='Convert', command=self.convert
    ) .grid(column=1, row=self.row(1), columnspan=2,paddy=(10,0))
    Button(
      frame, text='Quit', command=self.destroy
    ) .grid (column=3, row=self.row(), columnspan=2, padx=(10,0), pady=(10,0))

  def convert (self):
    try:
      val=self.money.get()
    except:
      return
    current=conversion_table [self.current.get()]
    aus_dollar=round (val*currency ['rate'], 1)
    messagebox.showinfo(
      'Results',
      f'Your {val} {currency["currency"]} = {aus_dollar} in Australian Dollars'
    )

  default(self, step=0):
    self.counter+=step
    return self.counter

if__name__=='__main__':
  gui=CurrencyConverter()
  gui.mainloop()


2022-09-30 19:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.