Dec-28-2018, 10:39 AM
Thank you very much!
I edited the code and came to this result:
And at the beginning there are two pictures shown. Why?
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.mainloopThe 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?