Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Feedback and help
#11
Thank you very much!

How are you going? Did you get it running?
Reply
#12
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)
Reply
#13
Okay, thank you!

Where do you run the code? I won't us the internal output shell of VSCode. Until a few weeks ago I also used VSCode, but then switched to the standard Python shell/IDLE, because of some running errors, I just couldn't fix.
Reply
#14
Idle is a very poor IDE (if it can even be called that), I don't know the reasoning behind python.org's packaging it with python. I have been using VSCode for well over a year now, and have never had an issue with it. So I tried running from command line using my virtual environment (after removing __pycache__). The file is being created by the PIL import!, and still an issue.
So one more attempt sans the virtual environment and still same problem.

It's something to do with the method of displaying image. numbers.cpython-37.pyc is still being created in __pycache__ by the PIL process. There's an issue with the way the image is being displayed, and It's been too long since I used tkinter to remember why (although I do remember having issues with pillow).

Perhaps someone else has some insight.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020