-2

I'm currently creating a GUI in order to turn a lot of individual instruments into one complete system. In def smuSelect(self) I create a list self.smuChoices I can use to call individual choices such as smuChoices[0] and it will return "2410(1)".

Once I call def checkBoxSetup it returns PY_VARxxx. I've tried searching the different forums and everything. I've seen mentions using the .get() which just gives me the state of the individual choice. The reason I want the actual string itself is I would like to use it in def testSetup(self) for the user to assign specific names to the individual machine, for example, 2410 = Gate.

My initial attempt was to create another variable smuChoice2 but I believe this is still changing the original list 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() 
Dj1612
  • 1
  • 3

2 Answers2

0

First of all getting PY_VARXX instead of what's in a variable class indicates the lack of get().

replace:

print(self.smuChoices[0])

with:

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

Secondly, if you want to display the value of a variable class on a label, button, etc. you could rather just use the textvariable option by simply assigning the variable class to it.

Replace:

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

with:

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

Your question is still a bit unclear to me but I will try to provide an answer to the best of my understanding.

As I understand it, you're trying to create a set of Checkbuttons for a given list of items. Below is an example of a method that takes items as an argument and returns a dictionary of checkboxes that have root as their parent:

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()

Additionally note that I haven't used any variable classes (BooleanVar, DoubleVar, IntVar or StringVar) as they seem to be redundant in this particular case.

Nae
  • 14,209
  • 7
  • 52
  • 79
  • The only thing the .get() method returns is a 1 or a 0 indicating whether or not the box is checked or not, I understand this. My problem lies in trying to get the actual machine name from the list. I believe something fundamentally (possibly when calling tk.IntVar() in def checkBoxSetup) is changing in def checkBoxSetup because prior to calling that I can get the smu name by saying self.smuChoices[0] without using .get(). I tried using the .get() function previously and all it returns is the state of the checkbox. Apologies for saying it worked when in fact it didn't – Dj1612 Jan 05 '18 at 15:38
  • @Dj1612 If only you were able to create a [mcve], it would've been much easier to understand what you're trying to achieve. – Nae Jan 05 '18 at 15:51
  • 1
    I didn't realize there was an actual definition for Minimal, complete and Verifiable example. I thought it was just shorten the code to the necessary definitions, this is my first post I apologize. Either way I think my edited code satisfies the requirements – Dj1612 Jan 05 '18 at 16:18
  • @Dj1612 Pleas re-verify your code, while it may have lost the redundant parts, it still appears that it has some mistakes. Please copy the exact code you've provided while verifying. – Nae Jan 05 '18 at 16:52
0

I was able to solve the problem by changing my list, smuChoices, to a dictionary then modifying

def checkBoxSetup(smuChoice2): 

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

to

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()

Previously I was replacing the variable with what I'm guessing is some identifier that tkinter uses to store the state which is why I was getting PYxxx.

Dj1612
  • 1
  • 3