I am trying to display a button over an image.The button appears to be in front of the image.
How can I set the image to the background of the app?
The button also needs to change his location, so the image should be permenant.
My code:
from Tkinter import *
class App:
def __init__(self,master):
frame = Frame(master)
master.geometry("400x200")
frame.pack()
self.hello_b = Button(master,text="ME",command=sys.exit, height=1, width=3,fg= "blue",bg = "green")
self.hello_b.pack()
photo = PhotoImage(file="unnamed.gif")
w = Label (master, image=photo)
w.place(x=0, y=0, relwidth=1, relheight=1)
w.photo = photo
w.pack()
root = Tk()
app = App(root)
root.mainloop()
The answer, which I suspected, is here: "[the code] packs a Label widget in a frame widget, and then places a Button in the upper right corner of the frame. The button will overlap the label." I tested like so
from tkinter import *
class App:
def __init__(self,master):
master.geometry("400x200")
photo = PhotoImage(file="C:/programs/Python34/temimage.png")
self.w = w = Label (master, image=photo)
w.photo = photo
w.pack()
self.hello_b = Button(master,text="ME",command=sys.exit, height=1,
width=3,fg= "white",bg = "green")
self.hello_b.place(x=200, y=5)
root = Tk()
app = App(root)
root.mainloop()
The button is indeed on top of the image. The x,y positions seems to be the upper left corner of the button. Adding
def move():
app.hello_b.place(x=100, y=100)
root.after(1000, move)
caused the button to jump after a second, as expected.
Related
import tkinter as tk
from tkinter import *
HEIGHT = 600
WIDTH = 600
root = tk.Tk()
def button_event1():
import ThreePlayers
print(ThreePlayers)
def button_event2():
import TwoPlayersGame
print(TwoPlayersGame)
def button_event3():
print("")
def button_event4():
print("")
def button_event5():
quit()
root = Tk()
root.title('Connect 4 Game')
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
L = Label(root, text="Welcome to 3P Connect 4!!!",font=("Ariel",20,"bold", "underline"))
L.config(anchor=CENTER)
L.pack()
button = tk.Button(root, text="3 Player Game", command=button_event1)
button.pack()
button = tk.Button(root, text="2 Player Game", command=button_event2)
button.pack()
button = tk.Button(root, text="1 Player Game", command=button_event3)
button.pack()
button = tk.Button(root, text="Options", command=button_event4)
button.pack()
button = tk.Button(root, text="QUIT", command=button_event5)
button.pack()
root.mainloop()
Above is my Code for a Tkinter GUI but I want the have the label at the center of the root/window how do I do that? Currently its sitting on top of the buttons everything else is fine the button events and such works
In my opinion, you should have used place or grid instead of pack. Because pack only gives few alignment options.
otherwise, maybe divide the main window into two frames then pack the label at the top of the lower frame
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
L = Label(root, text="Welcome to 3P Connect 4!!!",font=("Ariel",20,"bold", "underline"))
L.pack(side = TOP)
I wish this helps. but you should use grid for better alignment or place.
You can use the following commands to place the label in the center
L = Label(root, text="Welcome to 3P Connect 4!!!",font=("Ariel",20,"bold", "underline"))
# L.config(anchor=CENTER)
# L.pack()
L.place(x=HEIGHT/2, y=WIDTH/2, anchor="center")
Similarly, you can also use button.place(x=100, y=25) for buttons
REF: Tkinter: Center label in frame of fixed size?
Whenever I run this code with a background Image the button grid gets misplaced and pushed towards the bottom. Fortunately, it works as intended when no background is added .I want them to cover the background when executed. Pictures for reference are added below. Your help is highly appreciated.
# importing the module
import tkinter.messagebox
from tkinter import *
import random
# importing the module
# initialising tkinter
class window(Frame):
def __init__(self,master = None):
Frame.__init__(self,master)
self.master = master
# initialising tkinter
# creating the window
root = Tk()
app = window(root)
root.geometry("630x630")
root.title('Odd Even Game')
C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file = "BG.png")
background_label = Label(root,image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
frame = Frame(root)
frame.pack()
# creating the window
# image
level_1e = "p1.png"
level_1o = "pe.png"
level_2e = "r1.png"
level_2o = "re.png"
# image
def create_cards(odd_image,even_image,next_level,fno,order,suc,err,w,h):
rx = random.randint(0,order-1)
ry = random.randint(0,order-1)
for i in range(0,order):
for j in range(0,order):
if i == rx and j == ry:
create_button(i,j,suc,odd_image,next_level,fno,odd_image,w,h)
else:
create_button(i,j,err,even_image,next_level,fno,odd_image,w,h)
def second_level(fno):
fno.pack_forget()
frame2 = Frame(root)
frame2.pack()
suc = "Congratulations! You have cleared level 2..Keep Going Buddy!"
err = "Wrong Answer..Don't give up yet!"
create_cards(level_2o,level_2e,final_level,frame2,4,suc,err,157.5,157.5)
def final_level(fno):
fno.pack_forget()
root.geometry("700x700")
ap = App(root)
# creating a button function
def create_button(x,y,msg,picture,next_level,fno,odd,w,h):
if picture == odd:
image = PhotoImage(file=picture)
click = Button(fno, image=image, width= w, height=h, bd = 0,command = lambda : [score_update(),next_level(fno),tkinter.messagebox.showinfo( "Odd One Out Project",msg)])
click.image = image
click.grid( row = x, column = y)
else:
image = PhotoImage(file=picture)
click = Button(fno, image=image, width= w, height=h, bd = 0,command = lambda : [next_level(fno),tkinter.messagebox.showinfo( "Odd One Out Project",msg)])
click.image = image
click.grid( row = x, column = y)
# creating a button function
def create_frame(fno):
root.geometry("630x630")
fno.pack_forget()
frame = Frame(root)
frame.pack()
suc = "Congratulations! You have cleared level 1..Time to increas[![enter image description here][1]][1]e the difficulty!"
err = "Wrong Answer..Please Try again !!"
create_cards(level_1o,level_1e,second_level,frame,3,suc,err,200,200)
def intro():
root.geometry("630x630")
frame0 = Frame(root)
frame0.pack()
click = Button(frame0,text="Start!" ,command = lambda [create_frame(frame0),tkinter.messagebox.showinfo( "Odd One Out Project","The game has begun!!")])
click.pack()
intro()
# starting the widget
root.mainloop()
# starting the widget
The first image is the error. Second Image is the required output.
Note: I'm still a beginner in Python and Tkinter hence various terms and methods might be something out of my scope. Would be appreciated if taken into consideration.
In case needed, you might know that this is a tkinter project for picking the odd one out image out of A*A grid.
I got the answer myself so will be sharing for future use.
C = Canvas(root, bg="blue", height=250, width=300)
This part draws a canvas of 250*300 dimensions hence does not let the buttons draw over it.
just change it to,
C = Canvas(root, bg="blue", height=0, width=0)
for the desired result
Currently, I am creating a text adventure game. Now to make it a tiny bit better than a lot of them which use the python console, I am using Tkinter to make a GUI. Only it displays my background image for the start screen, but not the text I put there! Please help!
# writes the title as text in the window
titleFont = tkFont.Font(family = "Comic Sans MS", size = 20)
titleText = tkinter.Label(app, text="Gods of This World", font=titleFont)
titleText.pack()
# sets the background image to the games 3 colors (Red, Green, Grey)
C = tkinter.Canvas(app, height=300, width=250)
filename = tkinter.PhotoImage(file = "C:/Users/" + getpass.getuser() + "/Desktop/Gods of This World/net/godsofthisworld/ui/images/backgroundimage.png")
background_label = tkinter.Label(app, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
You put image after text - and you use .place(x=0, y=0, relwidth=1, relheight=1) - so text is hidden behind image.
Put it in different order - first image, next text.
import tkinter as tk
app = tk.Tk()
# first image
photo = tk.PhotoImage(file="image.png")
background_label = tk.Label(app, image=photo)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
# next text
titleText = tk.Label(app, text="Gods of This World")
titleText.pack()
app.mainloop()
BTW: label with text will have gray background - and you can't remove it. If you want text without background then use Canvas and canvas.create_image(), canvas.create_text() - without pack() and place()
import tkinter as tk
WIDTH = 800
HEIGHT = 600
app = tk.Tk()
canvas = tk.Canvas(width=WIDTH, height=HEIGHT)
canvas.pack()
photo = tk.PhotoImage(file="image.png")
canvas.create_image((WIDTH/2, HEIGHT/2), image=photo) #, anchor='center')
canvas.create_text((WIDTH/2, HEIGHT/2), text="Gods of This World") #, anchor='center')
app.mainloop()
I would like to explicitly set the terminal size. Is there a cross-platform way to do this?
I want to make a game, showing fixed-sized art.
I used Tkinter (Thanks Steven Rumbalski!), because I don't mind using GUI if it's cross-platform. Basically, I set a window geometry and the size of my "art" label:
from Tkinter import *
#Assign a variable to the shown image to be able to change it
displayed_image = "test.gif"
class App:
def __init__(self, master):
#Make a frame to enclose the art with a relief
self.topframe = Frame(master, borderwidth=5, pady=5, relief=RIDGE)
#Pack the frame to draw it
self.topframe.pack()
#This frame will contain the prompt
self.frame = Frame(master)
self.frame.pack()
photo = PhotoImage(file="../Art/"+displayed_image)
art = Label(self.topframe, width=1280, height=720, image=photo)
art.photo = photo
art.pack()
prompt = Entry(self.frame, width=1280)
prompt.pack()
root = Tk()
root.wm_geometry("%dx%d" % (1300, 780))
app = App(root)
root.mainloop()
root.destroy()
This seems like a pretty straightforward question, but i am having trouble displaying a jpg image when a button is clicked. Here is my code (without the button code for the sake of time):
from tkinter import *
#screen stuff here
canvas = Canvas(app)
canvas.grid(row = 0,column = 0)
photo = PhotoImage(file = "test.jpg")
canvas.create_image(0,0, image = photo)
def show_image():
global canvas
global photo
canvas.create_image(0,0, image = photo)
#button that calls the function down here
Thanks!
This works with Python2:
import Tkinter as tk
import ImageTk
def show_image():
x = canvas.create_image(125, 125, image=tk_img)
while True:
print('show')
canvas.itemconfigure(x, state=tk.NORMAL)
button.configure(text = 'Hide')
yield
print('hide')
canvas.itemconfigure(x, state=tk.HIDDEN)
button.configure(text = 'Show')
yield
root = tk.Tk()
canvas = tk.Canvas(root, width=250, height=250)
canvas.grid(row=0, column=0)
tk_img = ImageTk.PhotoImage(file='image.png')
button = tk.Button(
root, text="Show", command=show_image().next, anchor='w',
width=10, activebackground="#33B5E5")
button.grid(row=1, column=0)
root.mainloop()
In Python3, PhotoImage can open GIF, PPM/PGM images. To open other formats, you may need to install Pillow (a fork of the PIL project for Python3).