0

So, the thing is, I'm currently trying to make a minecraft tellraw command generator. I didn't make the menu yet, and I'm trying to display an image inside of canvas with TKinter. The canvas does display, and it does work. But when I try to print an image inside of it using the function "create_image", this error appears when I try to use the pack function :

Traceback (most recent call last):
  File "C:\Users\PC\Desktop\Python\Algorithms\Minecraft Generator\index.py", line 34, in <module>
    MAIN_MENU_TELLRAW_IMAGE.pack()
AttributeError: 'int' object has no attribute 'pack'

Here is my code :

from tkinter import *

# Window
window = Tk()
window.title('Minecraft Generator')
window.geometry("1080x600")
window.minsize(1080, 600)
window.maxsize(1080, 600)
window.iconbitmap('assets/img/logo.ico')

# Config
window.config(background='#627f1f')

#Composants

    # Main Menu Title
TITLE_FRAME = Frame(window, bg='#627f1f')
MAIN_MENU_TITLE = Label(TITLE_FRAME, text='Minecraft Generator' , font=("Courrier", 40), bg='#627f1f' , fg='white')
MAIN_MENU_SUBTITLE = Label(TITLE_FRAME, text='A Minecraft Generator for all needs !' , font=("Courrier", 15), bg='#627f1f' , fg='white')

    # Main Menu TellRaw
MAIN_MENU_TELLRAW_BUTTON = Button(TITLE_FRAME, text='Tellraw Command', bg='white' , fg='#627f1f' , font=("Courrier News", 15))
MAIN_MENU_TELLRAW_IMAGE = Canvas(window, width=157, height=100)
MAIN_MENU_TELLRAW_IMAGE = MAIN_MENU_TELLRAW_IMAGE.create_image((157, 100), image=PhotoImage(file='assets/img/tellraw_logo.png'))

# Packing

    # Title Frame
MAIN_MENU_TITLE.pack()
MAIN_MENU_SUBTITLE.pack()
TITLE_FRAME.pack(side='top')

    # TellRaw
MAIN_MENU_TELLRAW_BUTTON.pack(pady=25)
MAIN_MENU_TELLRAW_IMAGE.pack()

# Display Window
window.mainloop()

I reeally don't have a clue of why this error is happening... If someone could help me, it would be kind ^^

Cold Fire
  • 91
  • 5
  • 3
    Change `MAIN_MENU_TELLRAW_IMAGE = MAIN_MENU_TELLRAW_IMAGE.create_image(...)` to `MAIN_MENU_TELLRAW_IMAGE.create_image(...)` – TheLizzard Apr 29 '21 at 13:41
  • 1
    @TheLizzard is correct, the problem is create_image returns an integer, thus the error message – Andrea Apr 29 '21 at 13:41
  • Also why are you using capital letters for your varaible names. It makes your code hard to read. Read about [PEP8](https://www.python.org/dev/peps/pep-0008/) please – TheLizzard Apr 29 '21 at 13:42

1 Answers1

1

As @Andrea said:

<tkinter.Canvas>.create_image(...) returns an int that is used to identify the specific canvas item (its id). On the line: MAIN_MENU_TELLRAW_IMAGE = MAIN_MENU_TELLRAW_IMAGE.create_image(...) you replace MAIN_MENU_TELLRAW_IMAGE with an int. When you try to MAIN_MENU_TITLE.pack() you are trying to call the .pack() method of an int which doesn't exist and that raises an error

TheLizzard
  • 7,248
  • 2
  • 11
  • 31