-13

I want to make program to calculate gas prices, but my code gets an error.

from tkinter import *

root = Tk()
ment = StringVar()

root.geometry('450x450+500+300')
root.title('ბენზმთვლელი')


mlabel2 = Label(root, text='რეგულარი >> '+2.23+ 'GEL').pack()
mlabel3 = Label(root , text = 'ევრო რეგულარი >> '+2.29+' GEL').pack()
mlabel4 = Label(root, text="პრემიუმი >> " + 3.35 + " GEL").pack()
mlabel5 = Label(root, text='დიზელი >> ' + 2.25 + ' GEL').pack()
mlabel6 = Label(root, text='ევრო დიზელი >> '+2.33+' GEL').pack()

mlabel1 = Label(root, text="მიუთითეთ ბენზინის რაოდენობა(ლიტრებში)").pack()
mentry1 = Entry().pack()

mlabel7 = Label(root, text="მიუთითეთ ბენზინის დასახელება(ზემოთ ჩამოთვლილთაგან)").pack()
mentry2 = Entry().pack()

def gamotvla():
    if mlabel7 == mlabel3 or mlabel2 or mlabel4 or mlabel5 or mlabel6:
        print(mlabel1*mlabel7)
    else:
        print('რაღაც შეიყვანე შეცდომით!!')

mbutton = Button(root, text='გამოთვლა',fg="red",bg='black', command=gamotvla).pack()

root.mainloop()

Error:

   Traceback (most recent call last):
  File "/home/zura12337/Desktop/Python/Atom/tk.py", line 11, in <module>
    mlabel2 = Label(root, text='რეგულარი >> '+2.23+ 'GEL').pack()
TypeError: cannot concatenate 'str' and 'float' objects

Can you help me?

Prune
  • 76,765
  • 14
  • 60
  • 81

2 Answers2

1

This one line is more than enough to cause the problem:

text='რეგულარი >> '+2.23+ 'GEL'

2.23 is a floating-point value; 'GEL' is a string. What does it mean to add an arithmetic value and a string of letters?

If you want the string label 'რეგულარი >> 2.23 GEL', then write it just that way. If you want to construct it from the parts, then convert the value to string:

text = 'რეგულარი >> ' + str(2.23) + 'GEL'
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Hey ,there is one more problem ! :D Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1544, in __call__ return self.func(*args) File "/home/zura12337/Desktop/Python/Atom/tk.py", line 25, in gamotvla print(mlabel1*mlabel7) TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' – ზურა კურტანიძე Nov 27 '17 at 18:31
  • 1
    (1) That's a separate problem; (2) What have you done to resolve it? /// Please read and follow the posting guidelines in the help documentation. Post the [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) as a separate question ... *after* you have searched for the root cause: you're getting `None` as a return value from `Label`, code you did not supply. – Prune Nov 27 '17 at 18:50
-1

Let's look at the line in question, and identify a few problems with it. Here is the line:

mlabel2 = Label(root, text='რეგულარი >> '+2.23+ 'GEL').pack()

You are concatenating a string, a float, and another string. Easiest way to get around this is with some string format expression, like

mlabel2 = Label(root, text='რეგულარი >> {num} GEL'.format(num=2.23))

That would be a good solution for Python 3 code... Note that {num} is a formatting command that gets replaced with your float. There are other ways to handle this, but since you're using Python 3, I would go with that one. Plus since that number changes in your different Labels, I kept it separate so it's easy to work with.

Also, keep in mind that your mlabel2 is going to be None in your code, because of the .pack() command, which returns None. If you want to preserve mlabel2, you have to break the statement up into two...

mlabel2 = Label(root, .... GEL')
mlabel2.pack()

This is the same for most all of your widgets, including the Label's, Entry's, and Button. The if statement in your def gamotvla(): is also going to cause you problems, but let's start with these.

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32