Unframed Tkinter Label Image
This is my first post. I visit Stack Overflow frequently and I used to always find answers to all my questions, but not today.
I'm trying to display images as shortcuts in a window, but not the way I thought Tkinter would display them. In other words. I have some small images that need to be placed next to each other without spaces. But besides all my efforts, Tkinter always places a small border or gap (maybe 1-2 pixels) between two adjacent elements.
from tkinter import *
from tkinter import ttk
class MainWindow():
def __init__(self, mainWidget):
self.status_bar_text = StringVar()
self.status_bar_text.set('')
self.image_to_place = PhotoImage(file='my_image.png')
self.main_frame = ttk.Frame(mainWidget, width=768, height=480, padding=(0, 0, 0, 0))
self.main_frame.place(x=0, y=0)
self.status_bar = ttk.Label(mainWidget, width=768, border=1, anchor=W, relief=SUNKEN, textvariable=self.status_bar_text)
self.status_bar.place(x=0, y=480)
self.main_gui()
def main_gui(self):
i = 0
plate_cords = [[0, 0], [24, 0], [48, 0], [72, 0], [96, 0], [120, 0]]
for plate in plate_cords:
self.wall_label = ttk.Label(self.main_frame, image=self.image_to_place)
self.wall_label.place(x=plate_cords[i][0], y=plate_cords[i][1])
i += 1
del i
def main():
global root
root = Tk()
root.title('Title')
root.geometry('768x500+250+100')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
window = MainWindow(root)
window
root.mainloop()
if __name__ == '__main__':
main()
I've tried options like "borderwidth", "padding", "bordermode" and a few other tricks, and nothing works as I'm guessing. Thanks for any help and ideas.
source to share
There are two attributes that must be set to 0 (zero): borderwidth
and highlightthickness
. borderwidth
(along with relief
) defines the actual border of the widget. highlightthickness
also defines the sorting border - it is a rectangular ring that is visible when the widget has focus.
source to share
I have an image width of 237x37.
from tkinter import*
import tkinter as tk
from tkinter import font
top = Tk()
top.geometry("800x480")
top.title('FLOW')
C = Canvas(top, bg="#0026D1", height=480, width=800)
LabelsFont = font.Font(family='DejaVu Sans', size=10, weight='bold')
filenameLabel1 = PhotoImage(file = "/home/autorun/Desktop/pictures/štítok1.png")
Label1 = tk.Label(top, wraplength = 230, font=LabelsFont, fg="white", text="PRETLAK NA VSTUPE",image=filenameLabel1,borderwidth=0,compound="center",highlightthickness = 0,padx=0,pady=0)
Label1.place(x=15,y=90)
C.pack()
top.mainloop()
If there is no width and height in Label1.place method, you must use pady = 0, padx = 0, borderwidth = 0, highlightthickness = 0, or you must use Label1.place with image width and height with borderwidth = 0, highlightthickness = 0.
Second way in my code:
Label1 = tk.Label(top, wraplength = 230, font=LabelsFont, fg="white", text="PRETLAK NA VSTUPE",image=filenameLabel1,borderwidth=0,compound="center",highlightthickness = 0)
Label1.place(x=15,y=90,width=237,height=37)
source to share