在 Tkinter 中使用图像制作切换按钮

发布于 2024-09-11 10:45:18 字数 53 浏览 1 评论 0原文

我知道如何在 Tkinter 中使图像成为按钮,现在如何使图像成为类似于单选按钮的切换按钮?

I know how to make an image a button in Tkinter, now how do I make th image a toggle button similar to a radio button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-09-18 10:45:19

使用“indicatoron”设置为 False 的复选按钮。这将关闭小复选框,以便您只能看到图像(或文本),并且每次单击浮雕时都会在凸起和凹陷之间切换。

另一种方法是使用标签小部件并自行管理按钮点击。添加 <1> 的绑定并将浮雕更改为如果凸起则为凹陷,如果凹陷则为凸起。使用复选按钮的内置功能更容易,因为它还可以处理键盘遍历、激活等。

Use a checkbutton with "indicatoron" set to False. This will turn off the little checkbox so you only see the image (or text), and the relief will toggle between raised and sunken each time it is clicked.

Another way is to use a label widget and manage the button clicks yourself. Add a binding for <1> and change the relief to sunken if raised, and raised if sunken. It's easier to use the built-in features of the checkbutton, since it also handles keyboard traversal, activation, etc.

是你 2024-09-18 10:45:19

由于某种原因,tkinter 告诉我它不知道 indicatoron 选项。但幸运的是,还有另一种方法来获得切换行为:小部件 <代码>ttk::buttonttk::checkbuttonttk::radiobutton 支持 "Toolbutton" 样式。对于 ttk::checkbuttonttk::radiobutton,这使得它们的行为类似于切换按钮,甚至可以用于使用 创建互斥按钮ttk::单选按钮

import tkinter           # use "import Tkinter" for python 2
from tkinter import ttk  # use "import ttk" for python 2

tk = tkinter.Tk()
toggleBtn = ttk.Checkbutton(tk, text = 'Toogle me!', style = 'Toolbutton')
toggleBtn.pack()
tk.mainloop()

For some reason, tkinter told me it wouldn't know the indicatoron option. But fortunately, there is an alternative way to get a toggle behaviour: The widgets ttk::button, ttk::checkbutton and ttk::radiobutton support a "Toolbutton" style. In the case of ttk::checkbutton and ttk::radiobutton, this makes them behave like a toggle button and can even be used to create mutually exclusive buttons using ttk::radiobutton.

import tkinter           # use "import Tkinter" for python 2
from tkinter import ttk  # use "import ttk" for python 2

tk = tkinter.Tk()
toggleBtn = ttk.Checkbutton(tk, text = 'Toogle me!', style = 'Toolbutton')
toggleBtn.pack()
tk.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文