I gave up, for some reason (actually know reason, but not how to fix yet) pillow is giving error on linux mint.
code:
from tkinter import *
from random import randint
from PIL import Image, ImageTk
import os
class Window(Frame):
def __init__(self, master = None):
# make sure in src directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("Rolling the dice")
self.pack(fill=BOTH, expand=1)
#quitButton = Button(self, text= "quit", command=self.client_exit)
#quitButton.place(x=0, y=0)
#making the roll button
rollButton = Button(self, text= "Roll the dice", command = self.roll())
rollButton.pack(side=BOTTOM)
#making the menu
menu = Menu(self.master)
self.master.config(menu=menu)
#making the file cascade in menu with button exit
file = Menu(menu)
file.add_command(label="Exit", command=self.client_exit)
menu.add_cascade(label="File", menu=file)
edit = Menu(menu)
edit.add_command(label="Undo")
edit.add_command(label="Show Image", command=self.showImg)
edit.add_command(label="Roll the dice", command=self.roll())
menu.add_cascade(label= "Edit", menu=edit)
def showImg(self):
load = Image.open("pic.jpg")
render = ImageTk.PhotoImage(load)
# labels can be text or images
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
def roll(self):
randomNumber = str(randint(1, 6))
# If using python 3.6 or newer:
#png_name = f'dices_{randomNumber}.png'
#Otherwise:
png_name = 'dices_{}.png'.format(randomNumber)
load = Image.open(png_name)
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.pack(side=TOP)
def client_exit(self):
exit()
def main():
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop
if __name__ == '__main__':
main()
Error:
Traceback (most recent call last):
File "/media/larz60/Data-2TB/Projects/TryStuff/src/dice.py", line 80, in <module>
main()
File "/media/larz60/Data-2TB/Projects/TryStuff/src/dice.py", line 76, in main
app = Window(root)
File "/media/larz60/Data-2TB/Projects/TryStuff/src/dice.py", line 16, in __init__
self.init_window()
File "/media/larz60/Data-2TB/Projects/TryStuff/src/dice.py", line 27, in init_window
rollButton = Button(self, text= "Roll the dice", command = self.roll())
File "/media/larz60/Data-2TB/Projects/TryStuff/src/dice.py", line 63, in roll
load = Image.open(png_name)
File "/media/larz60/Data-2TB/Projects/TryStuff/try_venv/lib/python3.7/site-packages/PIL/Image.py", line 2620, in open
preinit()
File "/media/larz60/Data-2TB/Projects/TryStuff/try_venv/lib/python3.7/site-packages/PIL/Image.py", line 386, in preinit
from . import JpegImagePlugin
File "/media/larz60/Data-2TB/Projects/TryStuff/try_venv/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 41, in <module>
from . import Image, ImageFile, TiffImagePlugin
File "/media/larz60/Data-2TB/Projects/TryStuff/try_venv/lib/python3.7/site-packages/PIL/TiffImagePlugin.py", line 48, in <module>
from fractions import Fraction
File "/home/larz60/.pyenv/versions/3.7.1/lib/python3.7/fractions.py", line 6, in <module>
from decimal import Decimal
File "/home/larz60/.pyenv/versions/3.7.1/lib/python3.7/decimal.py", line 3, in <module>
from _decimal import *
AttributeError: module 'numbers' has no attribute 'Number'
That's because when loading PIL, it caches numbers.cpython-37.pyc to __pycache__ and this causes catch 22 error.
I'll try and figure out how to turn off __pycache__ in VSCode, and try again, but can't do it for a while (probably tonight, EST time)