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: An image and label are not appearing. emont 7 405 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  Transparent window background, but not text on it muzicman0 7 2,736 Feb-02-2024, 01:28 AM
Last Post: Joically
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 3,977 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] Updating tkinter text BliepMonster 5 5,669 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,303 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,559 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,669 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,866 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 3,840 Jan-07-2022, 10:10 PM
Last Post: finndude
  tkinter change the text of the checkbox zazas321 1 3,753 Sep-17-2021, 06:19 AM
Last Post: zazas321

Forum Jump:

User Panel Messages

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