Reputation: 190
this is my first post. I visit frequently stack overflow and I previously have always found answers for all my questions, but not today.
I try to display images as labels in window, but it is not how I thought Tkinter would display them. In other words. I have several small images, which should be place each next to another without any gap. But in besides all my efforts Tkinter always place small border or gap (probably 1-2 pixel), between two neighbor 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 tried options such us 'borderwidth', 'padding', 'bordermode' and few other tricks, and nothing seems to work as I intend it to do. Thanks for any help and ideas.
Upvotes: 4
Views: 14584
Reputation: 1
This post is becoming quite old but if people still read it, i've found out that setting the "border" attribute of the label to False works too (rather than doing it the "long way")
label = tk.Label(...., border = False)
Upvotes: 0
Reputation: 61
I have image width 237x37 height.
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 haven't in Label1.place width and height you must use pady=0, padx=0, borderwidth=0, highlightthickness = 0 or you must use Label1.place with width and height the picture with borderwidth=0, highlightthickness = 0.
The 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)
Upvotes: 4
Reputation: 385910
There are two attributes which need to be set to 0 (zero): borderwidth
and highlightthickness
. borderwidth
(along with relief
) defines the actual border of the widget. highlightthickness
also defines a border of sorts -- it is a rectangular ring that is visible when the widget has focus.
Upvotes: 11