【发布时间】:2016-12-27 12:10:03
【问题描述】:
【问题讨论】:
-
是的,您可以创建带有图像和文本的按钮,或者只是图像,带有或不带有凸起的边框,以及您想要的任何颜色。所有这些都记录在按钮小部件的选项中。
标签: python button tkinter python-3.5
【问题讨论】:
标签: python button tkinter python-3.5
对于一个 Tkinter 项目,我在 tkinter.Frame 之上创建了一个 TkinterCustomButton 类构建。它在tkinter.Canvas 上绘制所有形状。您可以像普通的tkinter.Button 一样使用它,但它是高度可定制的。您可以更改圆角半径、边框宽度和所有颜色:
因此,如果您希望您的 tkinter 按钮看起来像问题中的示例一样定制且更现代,则并不总是需要使用图像。
虽然这并不能真正回答问题,但也许这个例子对某人有帮助。
您可以在此处找到TkinterCustomButton 类和示例程序:
https://github.com/TomSchimansky/GuitarTuner/tree/master/documentation
最简单的例子是:
import tkinter
from tkinter_custom_button import TkinterCustomButton
app = tkinter.Tk()
app.geometry("300x200")
app.title("TkinterCustomButton")
def button_function():
print("Button pressed")
button_1 = TkinterCustomButton(text="My Button", corner_radius=10, command=button_function)
button_1.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
app.mainloop()
给出:
在 Windows 上,我体验到 tkinter 画布的渲染质量非常糟糕,所以圆形,尤其是细边框看起来不太好......
【讨论】:
TkinterCustomButton 上的选项。在 /test_custom_button.py 文件中,我将播放按钮图像添加到按钮 7 作为示例。您必须导入 Pil,加载图像,将其转换为 PhotoImage,然后使用参数 image 将其传递给 TkinterCustomButton。
有可能!
如果您查看button documentation,您可以使用图像显示在按钮上。
例如:
from tkinter import *
root = Tk()
button = Button(root, text="Click me!")
img = PhotoImage(file="C:/path to image/example.gif") # make sure to add "/" not "\"
button.config(image=img)
button.pack() # Displaying the button
root.mainloop()
这是一个将图像添加到按钮小部件的简化示例,您可以使用按钮小部件制作更多很酷的东西。
【讨论】: