<i id='v3T1a'><tr id='v3T1a'><dt id='v3T1a'><q id='v3T1a'><span id='v3T1a'><b id='v3T1a'><form id='v3T1a'><ins id='v3T1a'></ins><ul id='v3T1a'></ul><sub id='v3T1a'></sub></form><legend id='v3T1a'></legend><bdo id='v3T1a'><pre id='v3T1a'><center id='v3T1a'></center></pre></bdo></b><th id='v3T1a'></th></span></q></dt></tr></i><div id='v3T1a'><tfoot id='v3T1a'></tfoot><dl id='v3T1a'><fieldset id='v3T1a'></fieldset></dl></div>
  • <small id='v3T1a'></small><noframes id='v3T1a'>

  • <legend id='v3T1a'><style id='v3T1a'><dir id='v3T1a'><q id='v3T1a'></q></dir></style></legend>

      <tfoot id='v3T1a'></tfoot>
      • <bdo id='v3T1a'></bdo><ul id='v3T1a'></ul>

        为什么使用 StringVar 而不是 IntVar 时 Tkinter 的单选按钮都开始选中?

        Why do Tkinter#39;s Radio Buttons all Start Selected When Using StringVar but not IntVar?(为什么使用 StringVar 而不是 IntVar 时 Tkinter 的单选按钮都开始选中?)
            <tbody id='SEq8V'></tbody>
          <tfoot id='SEq8V'></tfoot>

          <small id='SEq8V'></small><noframes id='SEq8V'>

            • <bdo id='SEq8V'></bdo><ul id='SEq8V'></ul>

                1. <i id='SEq8V'><tr id='SEq8V'><dt id='SEq8V'><q id='SEq8V'><span id='SEq8V'><b id='SEq8V'><form id='SEq8V'><ins id='SEq8V'></ins><ul id='SEq8V'></ul><sub id='SEq8V'></sub></form><legend id='SEq8V'></legend><bdo id='SEq8V'><pre id='SEq8V'><center id='SEq8V'></center></pre></bdo></b><th id='SEq8V'></th></span></q></dt></tr></i><div id='SEq8V'><tfoot id='SEq8V'></tfoot><dl id='SEq8V'><fieldset id='SEq8V'></fieldset></dl></div>
                  <legend id='SEq8V'><style id='SEq8V'><dir id='SEq8V'><q id='SEq8V'></q></dir></style></legend>
                  本文介绍了为什么使用 StringVar 而不是 IntVar 时 Tkinter 的单选按钮都开始选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这里是一些示例代码,它创建了 4 个 Radiobutton,其中 2 个使用 int,2 个使用 str:

                  Here is some example code that creates 4 Radiobuttons, 2 using int and 2 using str:

                  from tkinter import *
                  
                  class test:
                      def __init__(self):
                          wind = Tk()
                          frame1 = Frame(wind)
                          frame1.pack()
                          self.v1 = IntVar()
                          self.v2 = StringVar()
                  
                          int1 = Radiobutton(frame1, text = 'int1', variable = self.v1, value = 1,
                                             command = self.ipress)
                          int2 = Radiobutton(frame1, text = 'int2', variable = self.v1, value = 2,
                                             command = self.ipress)
                  
                          str1 = Radiobutton(frame1, text = 'str1', variable = self.v2, value = '1',
                                             command = self.spress)
                          str2 = Radiobutton(frame1, text = 'str2', variable = self.v2, value = '2',
                                             command = self.spress)
                  
                          int1.grid(row = 1, column = 1)
                          int2.grid(row = 1, column = 2)
                  
                          str1.grid(row = 2, column = 1)
                          str2.grid(row = 2, column = 2)
                  
                          str1.deselect() #this didn't fix it
                          str2.deselect()
                  
                      def ipress(self):
                          print('int'+str(self.v1.get()))
                      def spress(self):
                          print('str'+self.v2.get())
                  
                  test()
                  

                  运行后,我有一个如下所示的框:

                  After running this I have a box that look like this:

                  出于某种原因,str 开始被选中,而 int 则没有.是否有一个原因?有什么解决办法吗?我知道我可以通过使用数值然后将它们转换为字符串来解决它,但我想先了解为什么会发生这种情况.

                  For some reason the str ones start selected while the int ones do not. Is there a reason for this? Is there any fix for it? I know I could work around it by just using number values and then converting them to strings but I'd like to understand why this is happening first.

                  如果这很重要,我正在使用 Windows 10.

                  I am using Windows 10 if that matters.

                  澄清一下,这些按钮在被点击后仍然可以正常工作.

                  for clarification, the buttons still work properly after they have been clicked on.

                  推荐答案

                  在第二组单选按钮的情况下,它们以三态模式"呈现.

                  In the case of the second set of radiobuttons, they are being rendered in "tri-state mode".

                  根据官方文档1:

                  如果变量的值与 tristateValue 匹配,则单选按钮是使用三态模式绘制的.该模式用于表示混合值或多个值.

                  If the variable's value matches the tristateValue, then the radiobutton is drawn using the tri-state mode. This mode is used to indicate mixed or multiple values.

                  说明

                  tristatevalue 的默认值为空字符串,StringVar 的默认值为空字符串.对于您的第二组单选按钮,变量值和 tristatevalue 都是相同的,因此您看到的是三态模式".

                  Explanation

                  The default for tristatevalue is the empty string, and the default value for a StringVar is the empty string. For your second set of radiobuttons both the variable value and the tristatevalue are the same so you are seeing the "tri-state mode".

                  IntVar 的情况下,变量的默认值为零.tristatevalue 仍然是空字符串.这两个是不同的,所以小部件不会出现在三态模式"中.

                  In the case of the IntVar, the default value of the variable is zero. The tristatevalue is still the empty string. Those two are different so the widget does not appear in "tri-state mode".

                  为了证明这一点,请将第一组单选按钮的 tristatevalue 设置为零,使其与关联变量的默认值匹配,您将看到它们的外观发生变化以匹配第二组.

                  To prove this, set tristatevalue to zero for the first set of radiobuttons so that it matches the default value of the associated variable and you will see that their appearance changes to match the second set.

                  int1 = Radiobutton(..., tristatevalue=0)
                  int2 = Radiobutton(..., tristatevalue=0)
                  

                  同样,您可以将第二组的 tristatevalue 设置为默认值以外的其他值,以便它们看起来像第一组:

                  Likewise, you could set the tristatevalue of the second set to something other than the default value so that they will look like the first set:

                  str1 = Radiobutton(..., tristatevalue="x")
                  str2 = Radiobutton(..., tristatevalue="x")
                  

                  解决方案

                  单选按钮的最佳做法是始终确保默认值对应于单选按钮值之一,除非您确实需要三态模式".

                  Solution

                  The best practice with radiobuttons is to always make sure the default value corresponds to one of the radiobutton values, unless you truly do want "tri-state mode".

                  在您的情况下,您应该将变量初始化为单选按钮之一的值:

                  In your case, you should initialize the variables to the value of one of the radiobuttons:

                  self.v1 = IntVar(value=1)
                  self.v2 = StringVar(value="1")
                  

                  ... 或者在你创建它们之后,通过 set:

                  ... or after you create them, via set:

                  self.v1.set(1)
                  self.v2.set("1")
                  

                  <小时>

                  1 链接指向 tcl/tk 手册页.Tkinter 只是 tcl/tk 的一个薄包装,这个文档是小部件行为方式的明确答案.


                  1 the link goes to the tcl/tk man pages. Tkinter is just a thin wrapper around tcl/tk, and this documentation is the definitive answer to how the widgets behave.

                  这篇关于为什么使用 StringVar 而不是 IntVar 时 Tkinter 的单选按钮都开始选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                    • <bdo id='nXS0P'></bdo><ul id='nXS0P'></ul>

                          <tbody id='nXS0P'></tbody>
                        <i id='nXS0P'><tr id='nXS0P'><dt id='nXS0P'><q id='nXS0P'><span id='nXS0P'><b id='nXS0P'><form id='nXS0P'><ins id='nXS0P'></ins><ul id='nXS0P'></ul><sub id='nXS0P'></sub></form><legend id='nXS0P'></legend><bdo id='nXS0P'><pre id='nXS0P'><center id='nXS0P'></center></pre></bdo></b><th id='nXS0P'></th></span></q></dt></tr></i><div id='nXS0P'><tfoot id='nXS0P'></tfoot><dl id='nXS0P'><fieldset id='nXS0P'></fieldset></dl></div>
                        <legend id='nXS0P'><style id='nXS0P'><dir id='nXS0P'><q id='nXS0P'></q></dir></style></legend>

                          <small id='nXS0P'></small><noframes id='nXS0P'>

                            <tfoot id='nXS0P'></tfoot>