tkinter - 从gif按钮/标签中删除边框/填充

时间:2016-04-12 05:23:58

标签: python tkinter

我知道有很多与此问题相关的信息,但我不能将其中的任何一项用于我的具体情况。

我正在尝试用GIF创建一个tkinter按钮菜单,我想要这些GIF相互冲洗,没有边框,没有间距。我已经能够使用常规按钮实现此目的,但不能使用带有图像文件的按钮。我用标签尝试了这个,没有“边框”,但即使将相关属性设置为“0”,我仍然无法摆脱填充。

我知道“bd”和“highlightthickness”属性,我很确定highlightthickness属性是特定于canvas小部件的,但我可能错了?无论哪种方式,我把它放在我能想到的任何地方,我仍然有这个恼人的边界。

有人能指出我在正确的方向吗?我是python和tkinter的新手,但编码相对舒服。这看起来几乎和html表等一样简单,但我花了几个晚上搜索互联网,我找不到任何答案,更不用说基于图像的按钮菜单没有间距问题的任何例子。

再次为这种困境的新手道歉。任何指导将不胜感激。谢谢!

使用“标签”小部件的代码示例:

from Tkinter import *

root = Tk()
root.wm_title("Window Title")
root.config(background = "#FFFFFF")

leftFrame = Frame(root, width=600, height = 800, bd=0, highlightthickness=0)
leftFrame.grid(row=0, column=0, padx=0, pady=0)

try:
    imageEx = PhotoImage(file = 'nav1.gif')
    imageEx2 = PhotoImage(file = 'nav2.gif')

    Label(leftFrame, image=imageEx).grid(row=1, column=0, padx=0, pady=0)
    Label(leftFrame, image=imageEx2).grid(row=2, column=0, padx=0, pady=0)

except:
    print("Image not found")

root.mainloop()

使用“Button”小部件的代码示例:

import Tkinter as tk

button_flag = True

def click():
    global button_flag
    if button_flag:
        button1.config(bg="white")
        button_flag = False
    else:
        button1.config(bg="green")
        button_flag = True

root = tk.Tk()

frame1 = tk.Frame(root)
frame1.pack(side=tk.TOP, fill=tk.X)
photo1 = tk.PhotoImage(file="nav1.gif")
button1 = tk.Button(frame1, compound=tk.TOP, width=320, height=55, image=photo1,
    text="", bg='green', command=click, bd=0, highlightthickness=0)
button1.pack(side=tk.LEFT, padx=0, pady=0)
button1.image = photo1


frame2 = tk.Frame(root)
frame2.pack(side=tk.TOP, fill=tk.X)
photo2 = tk.PhotoImage(file="nav2.gif")
button2 = tk.Button(frame2, compound=tk.TOP, width=320, height=55, image=photo2,
    text="", bg='green', command=click, bd=0, highlightthickness=0)
button2.pack(side=tk.LEFT, padx=0, pady=0)
button2.image = photo2


root.mainloop()

0 个答案:

没有答案
相关问题