返回PY\u VARxxx而不是预期的字符串

2024-06-25 23:09:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: textinimportselfdefbuttonvalroot
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()

你的问题我还是有点不清楚,但我会尽我所能给出一个答案。你知道吗

据我所知,您正试图为给定的项目列表创建一组Checkbutton。下面是一个方法示例,该方法将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。你知道吗

我可以通过将我的列表更改为字典然后修改来解决这个问题

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的原因。你知道吗

相关问题 更多 >