Python Forum
Tkinter TEXT background image
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter TEXT background image
#1
Photo 
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!
Reply
#2
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_)
Reply
#3
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.
Reply
#4
ignore this post
Reply
#5
(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?
Reply
#6
scrolled canvas
and
canvas create_image
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter change the text of the checkbox zazas321 1 143 Sep-17-2021, 06:19 AM
Last Post: zazas321
  How to redraw an existing image to a different image TkInter zazas321 6 516 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 743 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter text widget word wrap position chrisdb 6 1,933 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 1,763 Mar-10-2021, 04:21 PM
Last Post: Sir
  tkinter button image Nick_tkinter 4 1,133 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 4,321 Feb-06-2021, 03:35 PM
Last Post: joe_momma
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 1,753 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Latex image too big on tkinter using sympy 4096 1 977 Nov-05-2020, 08:05 AM
Last Post: DPaul
  tkinter: Image to Label Maryan 10 1,589 Oct-29-2020, 01:48 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020