Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Feedback and help
#3
Thank you very much!
I edited the code and came to this result:
from tkinter import *
from random import randint
from PIL import Image, ImageTk

class Window(Frame):
    def __init__(self, master = None):
        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_'+ randomNumber +'.png'
 
        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()

    

root = Tk()
root.geometry("400x300")

app = Window(root)

root.mainloop
The problem is still that if I run it I can't refresh the image, when I click on the Button.
And at the beginning there are two pictures shown. Why?
Reply


Messages In This Thread
Feedback and help - by tomX - Dec-28-2018, 08:48 AM
RE: Feedback and help - by Larz60+ - Dec-28-2018, 10:10 AM
RE: Feedback and help - by tomX - Dec-28-2018, 10:39 AM
RE: Feedback and help - by Larz60+ - Dec-28-2018, 06:13 PM
RE: Feedback and help - by tomX - Dec-28-2018, 07:39 PM
RE: Feedback and help - by Larz60+ - Dec-29-2018, 12:31 AM
RE: Feedback and help - by tomX - Dec-30-2018, 08:29 AM
RE: Feedback and help - by Larz60+ - Dec-30-2018, 10:02 AM
RE: Feedback and help - by tomX - Dec-30-2018, 02:01 PM
RE: Feedback and help - by Larz60+ - Dec-30-2018, 04:37 PM
RE: Feedback and help - by tomX - Dec-31-2018, 01:17 PM
RE: Feedback and help - by Larz60+ - Dec-31-2018, 05:02 PM
RE: Feedback and help - by tomX - Dec-31-2018, 08:57 PM
RE: Feedback and help - by Larz60+ - Dec-31-2018, 11:00 PM

Forum Jump:

User Panel Messages

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