Python Forum

Full Version: Tkinter TEXT background image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey there!
I'm making a program manager with Tkinter and I want to set the background image of the Text widget which contains the buttons to an image.
Here is the code:
from tkinter import *    
import os
def command(d):
   print(d)

a = Tk()
scroll = Scrollbar(a, orient="vertical")
text = Text(a, width=20, height=15, wrap="none", yscrollcommand=scroll.set)
scroll.place(x=0, y=0, height=270, anchor='nw')
text.place(x=15, y=0, anchor='nw')
scroll.configure(command=text.yview)
b = []
c = []
DEBUG = {}
DEBUG['random'] = ['hhwher', 'fgiwue', 'ewhrgj', 'wrejkh', 'fkhejw', 'qhkwje', 'hsajkd', 'asjkdh', 'haksdj', 'hadjsg', 'kdjsha', 'hkadjs', 'rweyiu', 'jdkhsa']
for x in range(len(os.listdir())):
    if os.path.isfile(os.listdir()[x]):
        b.append(os.listdir()[x])
    if os.path.isdir(os.listdir()[x]):
        c.append(os.listdir()[x])
b = DEBUG['random']
for x in b:
    x = Button(text, text=x, anchor='e', bg='black', font=('DIN Alternate', '15'), width=15, command=lambda j=x: command(j))
    text.window_create("end", window=x)
    text.insert("end", "\n")
text.configure(state=DISABLED)
a.mainloop()
(You can remove all the code containing the DEBUG variable.)
Output:
[Image: DCjFd.png]
I hope you can understand what I mean, thanks for all the help!
I don't understand- do you want a background image overlaid by the buttons or insert the image with the buttons? I don't the text widget is a great candidate for the first option, maybe a scrolled canvas with an image and buttons placed over.
the second try:
from PIL import Image, ImageTk
 
a = Tk()
img = Image.open("example.png")
image_ = ImageTk.PhotoImage(img)

scroll = Scrollbar(a, orient="vertical")
text = Text(a, width=25, height=15, wrap="none", yscrollcommand=scroll.set)
text.image_create("1.0", image=image_)
It is strange because the OP is using a Text object where sane people use a frame on a canvas. So in his app the background for the buttons is a text object. Interesting.

This talks a little about styles for a Text object.

https://effbot.org/tkinterbook/text.htm

I don't know if background image is supported. You could try bgstipple and see if that works for you.
ignore this post
(Nov-01-2020, 09:35 PM)joe_momma Wrote: [ -> ]I don't understand- do you want a background image overlaid by the buttons or insert the image with the buttons? I don't the text widget is a great candidate for the first option, maybe a scrolled canvas with an image and buttons placed over.
the second try:
from PIL import Image, ImageTk
 
a = Tk()
img = Image.open("example.png")
image_ = ImageTk.PhotoImage(img)

scroll = Scrollbar(a, orient="vertical")
text = Text(a, width=25, height=15, wrap="none", yscrollcommand=scroll.set)
text.image_create("1.0", image=image_)
how do I make a scrollable canvas then?