【问题标题】:Returning PY_VARxxx instead of expected string返回 PY_VARxxx 而不是预期的字符串
【发布时间】:2018-06-14 18:49:02
【问题描述】:

我目前正在创建一个 GUI,以便将许多单独的乐器变成一个完整的系统。在def smuSelect(self) 中,我创建了一个列表self.smuChoices,我可以使用它来调用诸如smuChoices[0] 之类的个人选择,它将返回"2410(1)"

一旦我调用def checkBoxSetup,它就会返回PY_VARxxx。我试过搜索不同的论坛和一切。我看到有人提到使用.get(),这只是给了我个人选择的状态。我想要实际字符串本身的原因是我想在def testSetup(self) 中使用它,以便用户为单个机器分配特定名称,例如2410 = Gate

我最初的尝试是创建另一个变量smuChoice2,但我相信这仍然会改变原始列表self.smuChoices

import tkinter as tk
import numpy as np
from tkinter import ttk


def checkBoxSetup(smuChoice2): #TK.INTVAR() IS CHANGING NAME OF SMUS NEED TO CREATE ANOTHER INSTANCE OF SELF.SMUCHOICES

    for val, SMU in enumerate(smuChoice2):

        smuChoice2[val] = tk.IntVar()
        b = tk.Checkbutton(smuSelection,text=SMU,variable=smuChoice2[val])
        b.grid()

root = tk.Tk()
root.title("SMU Selection")


"""
    Selects the specific SMUs that are going to be used, only allow amount up to chosen terminals.
    --> If only allow 590 if CV is picked, also only allow use of low voltage SMU (maybe dim options that aren't available)
    --> Clear Checkboxes once complete
    --> change checkbox selection method
"""
smuChoices = [
            "2410(1)",
            "2410(2)",
            "6430",
            "590 (CV)",
            "2400",
            "2420"
            ]
smuChoice2 = smuChoices
smuSelection = ttk.Frame(root)

selectInstruct = tk.Label(smuSelection,text="Choose SMUs").grid()
    print(smuChoices[0])    #Accessing list prior to checkboxsetup resulting in 2410(1)

checkBoxSetup(smuChoice2)

print(smuChoices[0])    #Accessing list after check box setup resulting in PY_VAR376
variableSMUs = tk.StringVar()

w7_Button = tk.Button(smuSelection,text="Enter").grid()

w8_Button = tk.Button(smuSelection,text="Setup Window").grid()

root.mainloop() 

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    我能够通过将列表 smuChoices 更改为字典然后修改

    来解决问题
    def checkBoxSetup(smuChoice2): 
    
     for val, SMU in enumerate(smuChoice2):
         smuChoice2[val] = tk.IntVar()
         b = tk.Checkbutton(smuSelection,text=SMU,variable=smuChoice2[val])
         b.grid()
    

    def checkBoxSetup(self): 
       for i in self.smuChoices:
           self.smuChoices[i] = tk.IntVar()
           b = tk.Checkbutton(self.smuSelection,text=i,variable=self.smuChoices[i])
           b.grid()
    

    以前我用我猜想是 tkinter 用来存储状态的某个标识符替换变量,这就是我得到 PYxxx 的原因。

    【讨论】:

      【解决方案2】:

      首先获得PY_VARXX 而不是变量类中的内容表明缺少get()

      替换:

      print(self.smuChoices[0])
      

      与:

      print(self.smuChoices[0].get())
      

      其次,如果您想在labelbutton 等上显示变量类的值,您可以只使用textvariable 选项,只需将变量类分配给它即可。

      替换:

      tk.Label(self.smuName,text=SMU).grid()
      

      与:

      tk.Label(self.smuName, textvariable=self.smuChoices[val]).grid()
      

      您的问题对我来说仍然有点不清楚,但我会尽我所能提供答案。

      据我了解,您正在尝试为给定的项目列表创建一组 Checkbuttons。下面是一个将items 作为参数并返回以root 为父复选框的字典的方法示例:

      import tkinter as tk
      
      def dict_of_cbs(iterable, parent):
          if iterable:
              dict_of_cbs = dict()
              for item in iterable:
                  dict_of_cbs[item] = tk.Checkbutton(parent)
                  dict_of_cbs[item]['text'] = item
                  dict_of_cbs[item].pack()            # it's probably a better idea to manage
                                                      # geometry in the same place  wherever
                                                      # the parent is customizing its
                                                      # children's layout
          return dict_of_cbs
      
      if __name__ == '__main__':
          root = tk.Tk()
          items = ("These", "are", "some", "items.")
          my_checkboxes = dict_of_cbs(items, root)
          root.mainloop()
      

      另外请注意,在这种特殊情况下,我没有使用任何变量类(BooleanVarDoubleVarIntVarStringVar)作为 they seem to be redundant

      【讨论】:

      • .get() 方法返回的唯一内容是 1 或 0 表示该框是否被选中,我理解这一点。我的问题在于尝试从列表中获取实际的机器名称。我相信从根本上来说(可能在 def checkBoxSetup 中调用 tk.IntVar() 时)在 def checkBoxSetup 中发生了变化,因为在调用之前我可以通过说 self.smuChoices[0] 而不使用 .get() 来获取 smu 名称。我之前尝试使用 .get() 函数,它返回的只是复选框的状态。很抱歉说它有效,而实际上它没有
      • @Dj1612 如果您能够创建一个minimal reproducible example,那么您将更容易理解您想要实现的目标。
      • 我没有意识到最小、完整和可验证示例的实际定义。我认为这只是将代码缩短为必要的定义,这是我的第一篇文章,我深表歉意。无论哪种方式,我都认为我编辑的代码满足要求
      • @Dj1612 请重新验证您的代码,虽然它可能丢失了多余的部分,但它似乎仍然存在一些错误。请复制您在验证时提供的确切代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 2020-10-15
      • 2019-09-15
      相关资源
      最近更新 更多