'How to do the basic math operation in Tkinter on python

I want to creat a molar calculator by typing the weight and molecular weight. But when I define the gen_molar (): , I cannot do the basic math calculation. For example,

def gen_molar ():
    w1 = weight_entry.get()
    mw2 = mw_entry.get()
    gen_molar= w1+mw2
    molar_show.config(text="Mole: " + gen_molar)

if the w1 is 180 and mw is 180, the result from the python shown 180180. (not 360)
I also cannot do division operation by the code.

from tkinter import*  

import pyperclip 
import math


win = Tk() 
win.title("Molar Calculators") 
win.geometry("400x300+200+200") 
win.config(bg="#323232")


title_text = Label(text="Molar Calculators", fg="skyblue",bg="#323232") 
title_text.config(font="Arial 20")
title_text.pack()

mw = Label(text="Molecular weight: ", fg="white",bg="#323232")
mw.pack()
mw_entry = Entry()
mw_entry.pack()

weight = Label(text="Weight(g): ", fg="white",bg="#323232")
weight.pack()
weight_entry= Entry()
weight_entry.pack()
molar_show = Label(text="", fg="white",bg="#323232" )
molar_show.pack()



def gen_molar ():
    w1 = weight_entry.get()
    mw2 = mw_entry.get()
    gen_molar= w1+mw2
    molar_show.config(text="Mole: " + gen_molar)

def copy():
    molar_show.cget("text")
    pyperclip.copy(molar_show) 


generate_btn = Button(text="Calculate", command= gen_molar) 
generate_btn.pack()

copy_btn = Button(text="copy result", command = copy)
copy_btn.pack()

win.mainloop()


Solution 1:[1]

The get()-method of tkinter-entry boxes returns a string. You can convert the input to float values using the float() function:

def gen_molar ():
    w1 = float(weight_entry.get())
    mw2 = float(mw_entry.get())
    gen_molar= w1+mw2
    molar_show.config(text="Mole: " + gen_molar)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikolaus