Reputation: 43
Im trying to make a simple game where you click the button and image of the drink displays on screen,
how to use the .create_image to display the image in my prefered position?
I know relx
/ rely
dont exactly work so what would be the best solutition
what exacty are the parameters passed in the canvas.create_image()?
from tkinter import *
from tkinter import messagebox
from PIL import Image
window = Tk()
window.geometry('1500x800')
window.resizable(1, 1)
window.config()
window.title('Bar simulator')
bg = PhotoImage(file='C:/Python/bar game/bar2.png')
bg_label = Label(window, image=bg)
bg_label.place(relwidth=1, relheight=1)
canvas = Canvas()
# canvas.pack()
# canvas.create_image(anchor='n', image=bg)
beer = PhotoImage(file='C:/Python/bar game/beer.png')
# beer_img = canvas.create_image(30, 13, image=beer)
# canvas.create_image(30, 30, image=beer)
title = Label(window, text='Welcome to the Bar!', bg='brown', font=('arial 30 bold'))
title.place(relx=0.345)
subtitle = Label(window, text='What would you like to drink?', bg='brown', font='arial 20 bold')
subtitle.place(relx=0.35, rely=0.1)
def alcohol():
msg = messagebox.askquestion('Age warning', 'Are you over 18?')
if msg == 'yes':
messagebox.showinfo('Age warning', 'enjoy your drink!')
return canvas.create_image(300, 13, anchor=NW, image=beer)
else:
messagebox.showinfo('Age warning', 'You are to old to drink!')
beer_button = Button(window, text='Beer', cursor='hand2', bg='yellow', command=alcohol, bd=5, width=10, fg='orange', font='arial 15 bold')
beer_button.place(relx=0.1, rely=0.2)
wine_button = Button(window, text='Wine', cursor='hand2', bg='#C70039', command=alcohol, bd=5, width=10, fg='white', font='arial 15 bold')
wine_button.place(relx=0.3, rely=0.2)
vodka_button = Button(window, text='Vodka', cursor='hand2', bg='blue', command=alcohol, bd=5, width=10, fg='white', font='arial 15 bold')
vodka_button.place(relx=0.5, rely=0.2)
sake_button = Button(window, text='Sake', cursor='hand2', bg='white', command=alcohol, bd=6, width=10, fg='blue', font='arial 15 bold')
sake_button.place(relx=0.7, rely=0.2)
window.mainloop()
Upvotes: 4
Views: 29594
Reputation: 15088
You could just simply read the docs for this, anyway, to create an image, you would use:
img = PhotoImage(file='image.png') #transparent image
canvas.create_image(x,y,image=img,anchor='ne')
This will create an image at (x,y) coordinates based on the -anchor option. And there are more options like -state, -tags, -disabledimage. Take a read through the docs. That is the only problem I could find here. Also noticed you are not passing in any coordinates onto:
canvas.create_image(anchor='n', image=bg)
Upvotes: 3