返回PY_VARxxx而不是预期的字符串

时间:2018-01-05 00:13:09

标签: python python-3.x tkinter

我目前正在创建一个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() 

2 个答案:

答案 0 :(得分:0)

首先获取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()

我的问题对我来说仍然有点不清楚,但我会尽力为我的理解提供答案。

据我了解,您正在尝试为给定的项目列表创建一组Checkbutton s。下面是一个方法示例,该方法将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情况下。

答案 1 :(得分:0)

我能够通过将我的列表 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的原因。