How to display an image outside a window - python

I am trying to display an image on my desktop without any boarder or window, like an image floating in the desktop. I also want to be able to control its position once it has been created, with the arrow keys or with a line of code that changes its position with some coordinate system of some sorts. I haven't found any method after some reaserch (not in Python, at least).
If that's not possible, please recommend another programming language that can.

You can use Tkinter for this.
I made a little example:
Python3
from tkinter import Toplevel, Tk, Label, PhotoImage
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100")
window.overrideredirect(1)
photo = PhotoImage(file="test.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
Python2
from Tkinter import Toplevel, Tk, Label
import ImageTk
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100") # create an window 500x500 pixel, 100 pixels from the upper left corner
window.overrideredirect(1) # Take the border away
photo = ImageTk.PhotoImage(file="test.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()

Related

The background doesn't round the button tkinter

I am using tkinter to create a GUI. I use PIL to import an image as the background. Here is my code:
root = tk.Tk()
root.title("DFUInfo-v1")
img = ImageTk.PhotoImage(Image.open("background.jpg"))
l=Label(image=img)
l.pack()
root.configure(bg='white')
root.geometry("490x280")
In my app, buttons are rounded. But when I use the image, the background does not match the round buttons, here is the image:
Can somebody help me pls? Thanks
Here is how you can create rounded "buttons" in tkinter (what determines the visible shape is how the image looks):
from tkinter import Tk, Canvas
from PIL import Image, ImageTk
# use your images here
# open your images (my preference is to use `.open` before initialising `Tk`)
img = Image.open('rounded.png')
# resize to match window geometry
bg = Image.open('space.jpg').resize((500, 400))
# the function to call when button clicked
def func(event=None):
print('clicked')
root = Tk()
root.geometry('500x400')
# here are the converted images
photo = ImageTk.PhotoImage(img)
bg = ImageTk.PhotoImage(bg)
# from now on this will be the "root" window
canvas = Canvas(root, highlightthickness=0)
canvas.pack(fill='both', expand=True)
# add background
canvas.create_image(0, 0, image=bg, anchor='nw')
# create button and you have to use coordinates (probably can make
# custom grid system or sth)
btn = canvas.create_image(250, 200, image=photo, anchor='c')
# bind the "button" to clicking with left mouse button, similarly
# as `command` argument to `Button`
canvas.tag_bind(btn, '<Button-1>', func)
root.mainloop()
Most of the explanation is in the code comments, but note that the bound sequence works for the whole image but images are always square so you can click the button being outside of the visible part but only as far as the image goes, it is most certainly possible to create a button that doesn't have such issues but that requires some math
Important (suggestion):
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.

python tkinter Can I make a widget in pixel? with text?

I want to make a text widget in pixel size. But always set on font size.
Here is my code:
import tkinter as tk
root = tk.Tk()
test_btn = tk.Button(root, text="test", height=10, width=10)
test_btn.pack()
root.mainloop()
I want to use pixel, but i can't so, I am using "place" method, so my code looks dirty.
I want to change them to "pixel size" and pack.
The width attribute is in units of characters if the button has text, but is in units of pixels if the button has an image. So, you can add a small transparent image to the button and set the compound option to allow both text and image at the same time. When you do that, width will be interpreted as pixels.
Example:
image = tk.PhotoImage(width=1, height=1)
button = tk.Button(root, image=image, text="Hello, world", compound="center",width=300)

How can I make my background image not block out any existing widgets in tkinter?

I am working on a tkinter project, where I set the background image (not a solid color). I was using .grid() for my widgets, but then I realized I need to display a widget at the very bottom of the tkinter window. To continue using grid for this, I separated my widgets into two frames; 1 frame that contained most of the widgets and one that just contained the widget I want to be displayed at the bottom. Then, I packed my frames, one on the top and the other at the bottom.
However, after I did this, my background image went on top of my existing widgets and blocked them out, so they can't be seen anymore. What can I do to fix this?
Here is the method I used to display the background photo:
from tkinter import *
filename = PhotoImage(file="image.png")
background_label = Label(root, image=filename) # root is the Tk() object
background_label.place(x=0, y=0, relwidth=1, relheight=1)
# Other code here
mainloop()

3.4 Python changing image position tkinter

I am making a small pop up to a text game I am making. I just want a small frame to pop up with some text and a picture to the far left but in a custom size and position. How would I do this? How do I insert text and change the image position and size, this is my current code, it works as in there are no errors and the image shows.
from tkinter import *
import time, sys
root = Tk()
root.configure(background='#16e116')
root.title('Pop Up')
root.geometry('300x200')
photo = PhotoImage(file='file.gif')
w = Label(root, image=photo)
w.pack()
root.mainloop()
If you want your image to be a different size (without it being deformed by resizing the widget) you have to make it the desired size in your image editor.
If you want to change the image position then use grid() where pack() has very limited placement options.
If you want text on a label, then use: text = Label(root, text="Hello world!"). This also comes with various options and styles such as font and it's size.
If you want the text over the image then use grid() and place it in the middle of the image by using rows and columns.
import tkinter
from tkinter import *
root = tkinter.Tk()
root.configure(background='#16e116')
root.title('Pop Up')
root.geometry('300x200')
photo = PhotoImage(file='file.gif')
w = Label(root, image=photo)
text = Label(root, text="Hello world!")
w.grid(row=3, column=3)
text.grid(row=3, column=3)
root.mainloop()
Although, the label will still have the background around it but you change the background colour using widgetname.config(bg="Colour")to make it suit your standards.
Now of course, I can't be sure the above will work because I don't have file.gif but you should also read tutorials before jumping straight into code to avoid confusion.

Create a text over an image using python Tkinter

I am creating an Arduino interface on Sublime using python Tkinter..
I need to show a text over an image. Located in the middle of the screen (512, 200). I don't know how to do it using this library
import Tkinter as tk
from Tkinter import *
root = tk.Tk()
root.geometry("1024x574")
root.title("window")
photo = tk.PhotoImage(file= r"hi.gif")
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=photo, anchor='nw')
text=['my text']
root.mainloop()
Any suggestions?
You need to create a tk label widget and add your text to it. Then you need to use the tk label option compound=.
Taken directly from http://effbot.org/tkinterbook/label.htm:
"compound=
Controls how to combine text and image in the label. By default, if an image or bitmap is given, it is drawn instead of the text. If this option is set to CENTER, the text is drawn on top of the image. If this option is set to one of BOTTOM, LEFT, RIGHT, or TOP, the image is drawn besides the text (use BOTTOM to draw the image under the text, etc.). Default is NONE."
The following is a minimal but working example that accomplishes what you asked for:
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
image = Image.open('hi.gif')
tk_image = ImageTk.PhotoImage(image)
label = tk.Label(root, text='Some Plain Text', image=tk_image, compound='center')
label.pack()
root.mainloop()

Categories

Resources