Tkinter themes and backgrounds [duplicate] - python

#import statements
from Tkinter import *
import tkMessageBox
import tkFont
from PIL import ImageTk,Image
Code to import image:
app = Tk()
app.title("Welcome")
image2 =Image.open('C:\\Users\\adminp\\Desktop\\titlepage\\front.gif')
image1 = ImageTk.PhotoImage(image2)
w = image1.width()
h = image1.height()
app.geometry('%dx%d+0+0' % (w,h))
#app.configure(background='C:\\Usfront.png')
#app.configure(background = image1)
labelText = StringVar()
labelText.set("Welcome !!!!")
#labelText.fontsize('10')
label1 = Label(app, image=image1, textvariable=labelText,
font=("Times New Roman", 24),
justify=CENTER, height=4, fg="blue")
label1.pack()
app.mainloop()
This code doesn't work. I want to import a background image.

One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.
For example:
background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.
Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:
background_label.image = background_image

A simple tkinter code for Python 3 for setting background image .
from tkinter import *
from tkinter import messagebox
top = Tk()
C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\\Users\\location\\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
top.mainloop

You can use this:
root.configure(background='your colour')
Example:-
import tkinter
root=tkiner.Tk()
root.configure(background='pink')

Related

Tkinter images and geometry creation

I've tried to do a star fractal drawing a star using tkinter and putting an image as a background.
from tkinter import *
from PIL import ImageTk, Image
import tkinter as tk
app = Tk()
app.title("Welcome")
img =Image.open('C:\\Users\\Stefa\\Downloads\\galaxy.jpeg')
bg = ImageTk.PhotoImage(img)
canvas_width=800
canvas_height=800
master = tk.Tk()
label = Label(app, image=bg)
label.place(x = 0,y = 0)
label2 = Label(app, text = "WELCOME TO OUR GALAXY",
font=("Times New Roman", 24))
label2.pack(pady = 50)
app.geometry(f"{canvas_width}x{canvas_height}")
can_widgt=Canvas(app, width=canvas_width, height= canvas_height)
can_widgt.pack()
points=[200,20,80,396,380,156,20,156,320,396]
can_widgt.create_polygon(points, outline='red',fill='cyan', width=6)
app.mainloop()
That's the code
However when i run it i want the star to be upon the background image. Any solutions for this ? Thanks
You need to create the image as a canvas object rather than as a label.
For example, this add the image to the top-left corner of the canvas, with the drawing on top of it:
can_widgt.create_image(0, 0, anchor="nw", image=bg)

Is it possible to give slideDown transition on Image in Tkinter?

Just like how we give effects on image in Jquery using the code
$('.img').slideDown('fast', 'linear', function() {});
Is there any way to give slideDown effect on canvas image in tkinter ?
This is my code.
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
frame = Frame(root)
frame.pack()
canvas = Canvas(frame, width=300, height=300, bd=0, highlightthickness=0, relief='ridge')
canvas.pack()
background = PhotoImage(file="background.png")
canvas.create_image(300,300,image=background)
my_pic = PhotoImage(file="start000-befored.png") #and on this image, I want to give slidedown effect.
canvas.create_image(50,50,image=my_pic, anchor=NW)
root.mainloop()

How can I set an image as a background in tkinter? [duplicate]

#import statements
from Tkinter import *
import tkMessageBox
import tkFont
from PIL import ImageTk,Image
Code to import image:
app = Tk()
app.title("Welcome")
image2 =Image.open('C:\\Users\\adminp\\Desktop\\titlepage\\front.gif')
image1 = ImageTk.PhotoImage(image2)
w = image1.width()
h = image1.height()
app.geometry('%dx%d+0+0' % (w,h))
#app.configure(background='C:\\Usfront.png')
#app.configure(background = image1)
labelText = StringVar()
labelText.set("Welcome !!!!")
#labelText.fontsize('10')
label1 = Label(app, image=image1, textvariable=labelText,
font=("Times New Roman", 24),
justify=CENTER, height=4, fg="blue")
label1.pack()
app.mainloop()
This code doesn't work. I want to import a background image.
One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.
For example:
background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.
Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:
background_label.image = background_image
A simple tkinter code for Python 3 for setting background image .
from tkinter import *
from tkinter import messagebox
top = Tk()
C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\\Users\\location\\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
top.mainloop
You can use this:
root.configure(background='your colour')
Example:-
import tkinter
root=tkiner.Tk()
root.configure(background='pink')

Add text entry widget over picture in tkinter using python

How can I make a text entry widget over picture in tkinter using python?
The code I am using:
from tkinter import *
from PIL import ImageTk, Image
import os
root = Tk()
root.title('Title')
E1 = Entry(root, bd = 5,show='*') # tried this line of code but it didn't worked
img = ImageTk.PhotoImage(Image.open("path/to/image.png"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
c = Button(text=" OK ")
c.place(relx=0.95, rely=0.98, anchor=SE)
root.mainloop()
It can make a button (OK) , why it can't make entry text widget?
You forgot to pack the Entry field, just try something like:
E1 = Entry(root, bd = 5,show='*')
E1.pack(side=BOTTOM, fill=BOTH, expand=YES)
And customize the position and behaviour as you prefer.

Displaying .jpg image when button is clicked in Tkinter?

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).

Categories

Resources