在 Tkinter 中使用图像制作切换按钮
我知道如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用“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.
由于某种原因,
tkinter
告诉我它不知道indicatoron
选项。但幸运的是,还有另一种方法来获得切换行为:小部件 <代码>ttk::button,ttk::checkbutton
和ttk::radiobutton
支持"Toolbutton"
样式。对于ttk::checkbutton
和ttk::radiobutton
,这使得它们的行为类似于切换按钮,甚至可以用于使用创建互斥按钮ttk::单选按钮
。For some reason,
tkinter
told me it wouldn't know theindicatoron
option. But fortunately, there is an alternative way to get a toggle behaviour: The widgetsttk::button
,ttk::checkbutton
andttk::radiobutton
support a"Toolbutton"
style. In the case ofttk::checkbutton
andttk::radiobutton
, this makes them behave like a toggle button and can even be used to create mutually exclusive buttons usingttk::radiobutton
.