如何在 Python Tkinter 中的按钮下方移动单选按钮的文本标签? [英] How can I move the text label of a radiobutton below the button in Python Tkinter?

查看:51
本文介绍了如何在 Python Tkinter 中的按钮下方移动单选按钮的文本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法将单选按钮的标签文本移动到不同的区域,例如在实际按钮下方.

以下是使用我正在使用的网格放置几个单选按钮的示例:

from tkinter import *从 tkinter 导入 ttk类 MainGUI(框架):def __init__(self, parent):self.parent = 父母Frame.__init__(self, parent, background="white")self.mainframe = ttk.Frame(root, padding="8 8 12 12")self.mainframe.grid(column=0, row=0,sticky=(N, W, E, S))self.mainframe.columnconfigure(0, weight=1)self.mainframe.rowconfigure(0, weight=1)ttk.Radiobutton(self.mainframe, text="Button 1", value=0).grid(column=1, row=2)ttk.Radiobutton(self.mainframe, text="Button 2", value=1).grid(column=2, row=2)ttk.Radiobutton(self.mainframe, text="Button 3", value=2).grid(column=3, row=2)ttk.Radiobutton(self.mainframe, text="Button 4", value=3).grid(column=4, row=2)如果 __name__ == '__main__':根 = Tk()root.geometry("300x100")app = MainGUI(root)root.mainloop()

下面是代码运行时的帧图像:

如您所见,默认设置是在按钮的一侧,但我正在寻找一种移动下方文本的方法,我似乎找不到任何解决此问题的方法!

谢谢,拉吉

解决方案

我的想法是尝试修改 ttk.RadioButton 的布局元素或元素的选项.参见

非常感谢 :) j_4321 基于我建议的方法构建的最新解决方案.我已经在我以前的代码中实现了它(见上文)并且它有效.我想强调以下几点:

  1. 我的代码显示了对 Radiobutton 的默认样式TRadiobutton"的布局所做的更改,而不是创建具有更改布局的新样式.这种差异的优点似乎克服了 j_4321 2nd 解决方案提到的缺点.也就是说,它适用于所有 ttk 主题('clam'、'alt'、'default'、'classic'),而不仅仅是适用于 'clam' 和 'alt'.至少这适用于我的 Linux 16.04 平台.
  2. 我之前代码中的错误是我创建了一个 s.layout() 的新实例(即 m = s.layout())并更改了它的布局,而不是实际更改默认 Radiobutton 样式的布局.layout('TRAdiobutton').我从 j_4321 的解决方案中了解到,可以通过在元组 ttk.Style.layout(widget 的默认样式名称)中添加小部件布局的更改细节作为第二项来更改默认样式的布局,即 ttk.Style.layout(小部件的默认样式名称,例如TRadiobutton",[小部件的布局细节更改]).
  3. 要将 Radiobutton 指示器的位置更改为显示在其标签的顶部,将 Radiobutton.indicator 的侧面"值更改为顶部"仅需要.对 Radiobutton.focus 的side"值的更改对 Radiobutton 的布局没有影响.

I'm wondering if there's a way to move the label text of a radiobutton to a different area, e.g. below the actual button.

Below is an example of a few radiobuttons being placed using grid that I'm using:

from tkinter import *
from tkinter import ttk


class MainGUI(Frame):

    def __init__(self, parent):
        self.parent = parent
        Frame.__init__(self, parent, background="white")
        self.mainframe = ttk.Frame(root, padding="8 8 12 12")
        self.mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        self.mainframe.columnconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=1)
        ttk.Radiobutton(self.mainframe, text="Button 1", value=0).grid(column=1, row=2)
        ttk.Radiobutton(self.mainframe, text="Button 2", value=1).grid(column=2, row=2)
        ttk.Radiobutton(self.mainframe, text="Button 3", value=2).grid(column=3, row=2)
        ttk.Radiobutton(self.mainframe, text="Button 4", value=3).grid(column=4, row=2)

if __name__ == '__main__':
    root = Tk()
    root.geometry("300x100")
    app = MainGUI(root)
    root.mainloop()

Below is an image of the frame when the code is run:

As you can see the default is to be on the side of the buttons, but I'm looking for a way to move the text below, I can't seem to find any solutions to this question!

Thanks, Raj

解决方案

My thought is to try amending the ttk.RadioButton's layout elements or element's option. See http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-layouts.html.

The layout of its element seems to consist of a label, that is inside the focus, that is inside indicator, that is inside the padding (See below variable rbLayout). The Radiobutton.indicator's side option value is left. The Radiobutton.label's anchor option appears blank. I think changing either of these options may get what you want. You would also have to add the option "style=your_customed_stylename" into your ttk.Radiobutton declaration.

>>> import tkinter.ttk as ttk
>>> s = ttk.Style()
>>> rb = ttk.Radiobutton(None, text='RB1')
>>> rbClass = rb.winfo_class()
>>> rbClass
'TRadiobutton'
>>> rbLayout = s.layout('TRadiobutton')
>>> rbLayout
[('Radiobutton.padding', {'sticky': 'nswe', 'children': [('Radiobutton.indicator', {'sticky': '', 'side': 'left'}), ('Radiobutton.focus', {'children': [('Radiobutton.label', {'sticky': 'nswe'})], 'sticky': '', 'side': 'left'})]})]
>>> type(rbLayout)
<class 'list'>

Update:

  1. I think the rbLayout variable shows that by default the Radiobutton.padding contains two children elements, i.e. Radiobutton.indicator and Radiobutton.focus, and they are positioned on the left side of Radiobutton.padding. Furthermore, Radiobutton.focus contains Radiobutton.label. Correction to my earlier explanation.
  2. To achieve the described layout, I think Radiobutton.indicator's 'side' key should have value 'top' and 'sticky' key should have value 'n'. Furthermore, Radiobutton.focus's 'side' key should have value 'bottom' and 'sticky' value should be 's'.
  3. I created a script to implemented the changes. See below code. However, it did not work. I am surprise the radiobutton layout did not change. Have not understood why it did not work.

Hope someone more knowledgeable can explain why the amendments to Radiobutton element's layout did not change to the required layout.

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.geometry("300x100")

s = ttk.Style()
m = s.layout('TRadiobutton')
print('TRadiobutton.Layout : Default')
print(m , "\n")

# Change to default Radiobutton.indicator elements
list(list(m[0])[1]['children'][0])[1]['side'] = 'top'
list(list(m[0])[1]['children'][0])[1]['sticky'] = 'n'

# Change to default Radiobutton.focus elements
list(list(m[0])[1]['children'][1])[1]['side']='bottom'
list(list(m[0])[1]['children'][1])[1]['sticky']='s'

print('TRadiobutton.Layout : Amended')
print(m, "\n")

frame1 = ttk.Frame(root)
frame1.grid()
rb1 = ttk.Radiobutton(frame1, text="Button 1")
rb1.grid()

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

Update 2 : SOLUTION

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.geometry("300x100")

s = ttk.Style()
s.theme_use('default')

print('TRadiobutton.Layout : Default')
print(s.layout('TRadiobutton'), "\n")

s.layout('TRadiobutton',
         [('Radiobutton.padding',
           {'children':
            [('Radiobutton.indicator', {'side': 'top', 'sticky': ''}), # Just need to change indicator's 'side' value
             ('Radiobutton.focus', {'side': 'left',
                                    'children':
                                    [('Radiobutton.label', {'sticky': 'nswe'})],
                                    'sticky': ''})],
            'sticky': 'nswe'})])

print('TRadiobutton.Layout : Amended')
print(s.layout('TRadiobutton'), "\n")

frame1 = ttk.Frame(root)
frame1.grid()
rb1 = ttk.Radiobutton(frame1, text="Button 1")
rb1.grid()

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

A big thanks :) to j_4321 for his latest solution building on my suggested approach. I have implemented it (see above) to my previous code and it works. I like to highlight the following:

  1. My code shows changes made to the layout of the Radiobutton's default style 'TRadiobutton' instead of creating a new Style with a changed layout. The advantage of this difference appears to overcome the shortcoming mention by j_4321 2nd solution. That is, it works on all ttk themes ('clam', 'alt', 'default', 'classic') and not just on 'clam' and 'alt'. At least this works on my Linux 16.04 platform.
  2. The mistake in my earlier code was that I had created a new instance of s.layout() (i.e. m = s.layout()) and changed it's layout, instead of actually changing the layout of the default Radiobutton style s.layout('TRadiobutton'). I learned from j_4321's solution that one can change the layout of a default style by adding the changed details of a widget's layout as the 2nd term in the tuple ttk.Style.layout(widget's default style name), i.e. ttk.Style.layout(widget's default style name e.g. 'TRadiobutton', [changed layout details of widget]).
  3. To change the Radiobutton indicator's location to appear at the top of it's label, change to the Radiobutton.indicator's 'side' value to 'top' is only needed. Changes to the Radiobutton.focus's 'side' value had no effect on Radiobutton's layout.

这篇关于如何在 Python Tkinter 中的按钮下方移动单选按钮的文本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆