0

When I try running the program below, it shows me this error :

Program:

import tkinter as tk

class RootWindow() :

    def __init__(self) :
        root=tk.Tk()
        self.root=root
        root.title("Isekai no yusha")
        root.wm_iconbitmap(bitmap = "Icon.xbm")
        root.configure(bg="black")
        root.resizable(width=False, height=False)

        screenWidth, screenHeight=root.winfo_screenwidth(), root.winfo_screenheight()
        screenWidth, screenHeight=int(screenWidth/2)-400, int(screenHeight/2)-200
        self.screenWidth, self.screenHeight=screenWidth, screenHeight

        #RootWindow.StartMenu(self)

        rootSize=(f"800x400+{screenWidth}+{screenHeight}")
        root.geometry(rootSize)
        root.mainloop()

    #def StartMenu(self) :
    #   newGameButton=tk.Button(self.root, bitmap=("new_game_icon.xbm"), bg="black", fg="white", border=False, command=self.root.destroy)
    #    newGameButton.pack()
    #    newGameButton.place(x=10, y=10)

master=RootWindow()

Error:

Exception has occurred: _tkinter.TclError
bitmap "new_game_icon.xbm" not defined
  File "D:\programmation\IDLE RPG\Test2.py", line 24, in StartMenu
    newGameButton=tk.Button(self.root, bitmap=("new_game_icon.xbm"), bg="black", fg="white", border=False, command=self.root.destroy)
  File "D:\programmation\IDLE RPG\Test2.py", line 17, in __init__
    RootWindow.StartMenu(self)
  File "D:\programmation\IDLE RPG\Test2.py", line 28, in <module>
    master=RootWindow()

I already tried changing file format to png, bmp and xbm but all of them didn't work( the file is already in the working folder and I already checked if the name between the file and the name in the code match). Also, when I try running the program without the Button, the icon in the top left corner of the window isn't showing, I get instead the image of a file. (Screenshot below).

Window icon:

Window icon

IDE used : Visual Code Studio, OS : Windows 10

Sorry for my poor language, I'm a french student.

1 Answers1

0

If I've understand what you want to do.

import tkinter as tk

class RootWindow() :

    def __init__(self) :
        root=tk.Tk()
        self.root=root
        root.title("Isekai no yusha")

        imgicon = tk.PhotoImage(file='icon.png')
        root.call('wm', 'iconphoto', root._w, '-default', imgicon)   
        #root.wm_iconbitmap(bitmap = "Icon.xbm")
1966bc
  • 1,148
  • 1
  • 6
  • 11
  • Thank you, now it works with your changes but I keep having an issue with the bitmap of the widget button, do I need to do the same as you did but applying it to the button ? – Ciurte Sergiu Mar 17 '19 at 17:00
  • wath button are you referrig? – 1966bc Mar 17 '19 at 17:22
  • Sorry for the wait, I am referring to this button, see below : – Ciurte Sergiu Mar 18 '19 at 06:08
  • #def StartMenu(self) : # newGameButton=tk.Button(self.root, bitmap=("new_game_icon.xbm"), bg="black", fg="white", border=False, command=self.root.destroy) # newGameButton.pack() # newGameButton.place(x=10, y=10) – Ciurte Sergiu Mar 18 '19 at 06:08
  • @CiurteSergiu: *"issue with the bitmap of the widget button"*: Yes. Read also [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/a/16424553/7414759) – stovfl Mar 18 '19 at 16:29