[英]Tkinter Label image without border
這是我的第一篇文章。 我經常訪問堆棧溢出,我以前總是找到所有問題的答案,但今天不是。
我嘗試在窗口中將圖像顯示為標簽,但這並不是我認為Tkinter會如何顯示它們。 換一種說法。 我有幾個小圖像,它們應該彼此相鄰,沒有任何間隙。 但除了我所有的努力之外,Tkinter總是在兩個相鄰元素之間放置小邊框或間隙(可能是1-2像素)。
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()
我嘗試了諸如'borderwidth','padding','bordermode'和其他一些技巧之類的選項,似乎沒有什么能像我打算那樣工作。 感謝您的幫助和想法。
有兩個屬性需要設置為0(零): borderwidth
和highlightthickness
。 borderwidth
(以及relief
)定義窗口小部件的實際邊框。 highlightthickness
還定義了各種邊框 - 它是一個矩形環,當窗口小部件具有焦點時可見。
我的圖像寬度為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()
如果沒有在Label1.place寬度和高度,你必須使用pady = 0,padx = 0,borderwidth = 0,highlightthickness = 0或者必須使用Label1.place,寬度和高度為borderwidth = 0的圖片,highlightthickness = 0 。
我的代碼中的第二種方式:
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)
對我來說這更好:
Label(..., state='normal')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.