Home > Software engineering >  I am making a calculator and I get TypeError: can only concatenate str (not "int") to str
I am making a calculator and I get TypeError: can only concatenate str (not "int") to str

Time:10-31

Here are the codes

from tkinter import *
from PIL import *
#============================= <Main> =============================
window = Tk()
window.geometry('680x815')
window.title('Calculator')
window.resizable(width=False,height=False)
window.configure(bg = 'red')
photo_bg = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.png')
label_bg = Label(window,image=photo_bg)
label_bg.place(x=30,y=0, height=780)
#=======================phbut=======================

photo_0 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.0.png')

photo_1 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.1.png')

photo_2 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.2.png')

photo_3 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.3.png')

photo_4 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.4.png')

photo_5 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.5.png')

photo_6 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.6.png')

photo_7 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.7.png')

photo_8 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.8.png')

photo_9 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.9.png')

photo_0 = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.0.png')

photo_p = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc...png')

photo_c = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.ac.png')

photo_per = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.%.png')

photo_div = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.div.png')

photo_minplu = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc. -.png')

photo_eq = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.equ.png')

photo_mini = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.min.png')

photo_plus = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.plus.png')

photo_mult = PhotoImage(file=r'C:\Users\Lenovo\Desktop\behrad\calc.mult.png')

#=======================button=======================
button_0 = Button(window,photo_0)

button_1 = Button(window,photo_1)

button_2 = Button(window,photo_2)

button_3 = Button(window,photo_3)

button_4 = Button(window,photo_4)

button_5 = Button(window,photo_5)

button_6 = Button(window,photo_6)

button_7 = Button(window,photo_7)

button_8 = Button(window,photo_8)

button_9 = Button(window,photo_9)

button_p = Button(window,photo_p)

button_c = Button(window,photo_c)

button_per = Button(window,photo_per)

button_div = Button(window,photo_div)

button_minplu = Button(window,photo_minplu)

button_eq = Button(window,photo_eq)

button_mini = Button(window,photo_mini)

button_plus = Button(window,photo_plus)

button_mult = Button(window,photo_mult)
#=======================</Main>=======================

window.mainloop()

I receive the error in this part

button_0 = Button(window,photo_0)

button_1 = Button(window,photo_1)

button_2 = Button(window,photo_2)

button_3 = Button(window,photo_3)

button_4 = Button(window,photo_4)

button_5 = Button(window,photo_5)

button_6 = Button(window,photo_6)

button_7 = Button(window,photo_7)

button_8 = Button(window,photo_8)

button_9 = Button(window,photo_9)

button_p = Button(window,photo_p)

button_c = Button(window,photo_c)

button_per = Button(window,photo_per)

button_div = Button(window,photo_div)

button_minplu = Button(window,photo_minplu)

button_eq = Button(window,photo_eq)

button_mini = Button(window,photo_mini)

button_plus = Button(window,photo_plus)

button_mult = Button(window,photo_mult)

here is the error

Traceback (most recent call last):
  File "c:\Users\Lenovo\Desktop\calc.py", line 55, in <module>
    button_0 = Button(window,photo_0)
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 2679, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 2595, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 2566, in _setup
    if 'name' in cnf:
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 4105, in __getitem__
    return self.tk.call(self.name, 'cget', '-'   key)
TypeError: can only concatenate str (not "int") to str

How can I correct the error?

CodePudding user response:

As @derek suggested in the comments, you may try button_0 = Button(windows, image=photo_0)

CodePudding user response:

You need to use something like this:

photo_0 = PhotoImage(file="calc.0.png")
button_calc = tk.Button(root, text="ButtonText", image=photo_0 , command=print_hello)
button_calc.pack() 

where photo_0 is your image, images and print_hello is the function for button use.

  • Related