OptionMenu callback not passing string or variable as expected - python

I have a simple form that I have built using tkinter. One of my widgets is a text option menu that allows the user to select 1 of 4 corporate or entity types. The widget straight after that requires the corporate or entity registration number
Logically if the user selects "Sole Proprietor", there is no reg number required as Sole Proprietors don't have reg numbers, so if this the case and the user selects "Sole Proprietor", I want the reg number widget to disappear.
So what I do is trace the OptionMenu Variable and if and pass it to the call back as can be seen below:
# Organisation Type (Drop List)
CurrentRowXAlignment += 3
OrgTypeLabel = tk.Label(SetUpWindow, text="Organisation Type:", font=('​Helvetica', 11, 'bold'))
OrgTypeLabel.place(x=ColumnXAlignment1, y=CurrentRowXAlignment, anchor=LabelAnchors)
OrgType = tk.StringVar(SetUpWindow)
OrgType.set(client_data_list[2])
OrgTypeEntry = tk.OptionMenu(SetUpWindow, OrgType, "Private Company (\"(Pty) Ltd\")",
"Public Company (\"Limited\")",
"Closed Corporation (\"cc\")", "Sole Proprietor")
OrgTypeEntry.place(x=ColumnXAlignment2 - 2, y=CurrentRowXAlignment - 3, anchor=EntryBoxAnchors)
OrgTypeEntry.bind("<Tab>",MovetoNextField)
OrgType.trace("w", lambda *args, org_type=OrgType.get(): ShowRegNumber(org_type, *args))
CurrentRowXAlignment += RowGap
The call back function is as follows:
def ShowRegNumber(org_type, *args):
if org_type == "Sole Proprietor":
CoRegNumLabel.forget()
CoRegNumEntry.forget()
else:
pass
For some reason the org-type is not passing - I have tried to debug and it keeps passing '' and therefore is keeps going to the "else" no matter what option on the menu is selected.
Anyone have an idea of what I am doing wrong?

When you use in lambda
org_type=OrgType.get()
then it gets value OrgType.get() only once - at start - and it assign this value.
You could use
lambda *args: ShowRegNumber( OrgType.get(), *args)
but more readable is to use it in function
def ShowRegNumber(*args):
org_type = OrgType.get()
but even better is to use
tk.OptionMenu(..., command=ShowRegNumber)
and then you get selected value as the only argument in ShowRegNumber
def ShowRegNumber(org_type):
Minimal working example
import tkinter as tk
# --- functions ---
def show_reg_number(selected):
print(selected)
# --- main ---
setup_window = tk.Tk()
client_data_list = [
'Private Company ("(Pty) Ltd")',
'Public Company ("Limited")',
'Closed Corporation ("cc")',
'Sole Proprietor',
]
org_type = tk.StringVar(setup_window)
org_type.set(client_data_list[2])
org_type_entry = tk.OptionMenu(setup_window, org_type, *client_data_list, command=show_reg_number)
org_type_entry.pack()
setup_window.mainloop()
BTW:
I used lower case names for variables and function because of PEP 8 -- Style Guide for Python Code

Related

Issue retrieving values from objects created by button function

I'm working on a small project and I'm having issues retrieving the values stored in combo boxes. The program has a "plus" button that creates additional boxes beneath the existing ones. They are created by calling a "create" function that makes a new instance of the ComboBox class, where the box is created and put onto the screen. A separate "submit" function is then supposed to loop through and retrieve all of the box values and store them in a list. My main flaw is that I used data in the variable names, but I have no clue how else to do this in this scenario. Does anyone have an alternative solution?
(there are some off screen variables that are show used here as parameters, but there are definitely not the source of the issue)
class ComboBox:
def __init__(self, master, counter, fields):
self.master = master
self.counter = counter
self.fields = fields
self.field_box = ttk.Combobox(width=20)
self.field_box["values"] = fields
self.field_box.grid(row=counter + 1, column=0, pady=5)
def get_value(self):
value = self.field_box.get()
return value
def create():
global entry_counter
name = "loop"+str(entry_counter-1)
name = ComboBox(window, entry_counter, fields)
values.append(name.get_value())
entry_counter += 1
def submit():
for i in range(1, entry_counter):
name = "loop" + str(entry_counter-1)
values.append(name.get_value())
For example, if I created 2 boxes and selected the options "test1" and "test2" I would want the my values list to contain ["test1, "test2"]
Not sure I understand the question right, but I guess you are asking about how to loop throw all instances of ComboBox. You can just create an global array, append new instance into it in create() method:
comboboxes = []
def create():
...
comboboxes.append(new_instance)
def submit():
for combobox in comboboxes:
...
You're on the right track with .get(). I believe your solution is that your get_value function also needs an event parameter:
def get_value(self, event):
value = self.field_box.get()
return value
See the following:
Getting the selected value from combobox in Tkinter
Retrieving and using a tkinter combobox selection

Replace text in textField with object name - Pymel

I've realized that there were similar questions located
here:
textfield query and prefix replacing
and
here:
Python - Change the textField after browsing - MAYA
However, these do not address the issue if you have two definitions and need the text in the textField to be queried (actually CHANGE the text in the textField).
I know from experience that doing what I have below in MelScript actually works, but for the sake of Python, and learning how to do it in Python, it seems to not work. Am I missing something? Do I need a lambda to get the name of the object selected and query the textField?
I have an example (a snip-bit of what needs to be fixed):
from pymel.core import *
def mainWindow():
window('myWin')
columnLayout(adj=1)
button('retopoplz', ann='Select a Mesh to Retopologize', bgc=[.15,.15,.15],
l='START RETOPOLOGY', c='Retopo(TextToMakeLive)')
TextToMakeLive = textField(ann='Mesh Selected', bgc=[.2,0,0],
edit=0, tx='NONE')
setParent('..')
showWindow('myWin')
def Retopo(TextToMakeLive):
#This tool selects the object to retopologize
MakeLiveField = textField(TextToMakeLive, q=1, tx=1)
MakeSelectionLive = (ls(sl=1))
if MakeSelectionLive is None:
warning('Please select an object to retopologize')
if MakeSelectionLive == 1:
TextToMakeLive = textField(TextToMakeLive, ed=1,
tx=MakeSelectionLive,
bgc=[0,.2,0])
shape = ls(s=MakeSelectionLive[0])
setAttr((shape + '.backfaceCulling'),3)
createDisplayLayer(n='RetopoLayer', num=1, nr=1)
makeLive(shape)
print('Retopology Activated!')
else:
warning('Select only ONE Object')
mainWindow()
GUI objects can always be edited -- including changing their commands -- as long as you store their names. So your mainWindow() could return the name(s) of gui controls you wanted to edit again and a second function could use those names to change the looks or behaviors of the created objects.
However, this is all much easier if you use a python class to 'remember' the names of the objects and any other state information: it's easy for the class to 'see' all the relevant info and state. Here's your original converted to classes:
from pymel.core import *
class RetopoWindow(object):
def __init__(self):
self.window = window('myWin')
columnLayout(adj=1)
button('retopoplz',ann='Select a Mesh to Retopologize', bgc=[.15,.15,.15],l='START RETOPOLOGY', c = self.do_retopo)
self.TextToMakeLive=textField(ann='Mesh Selected', bgc=[.2,0,0],edit=0,tx='NONE')
def show(self):
showWindow(self.window)
def do_retopo(self, *_):
#This tool selects the object to retopologize
MakeLiveField= textField(self.TextToMakeLive,q=1,tx=1)
MakeSelectionLive=(ls(sl=1))
if MakeSelectionLive is None:
warning('Please select an object to retopologize')
if len( MakeSelectionLive) == 1:
TextToMakeLive=textField(self.TextToMakeLive,ed=1,tx=MakeSelectionLive,bgc=[0,.2,0])
shape=ls(s=MakeSelectionLive[0])
setAttr((shape+'.backfaceCulling'),3)
createDisplayLayer(n='RetopoLayer',num=1,nr=1)
makeLive(shape)
print('Retopology Activated!')
else:
warning('Select only ONE Object')
RetopoWindow().show()
As for the callbacks: useful reference here
You need to assign the command flag AFTER you have created your textField to be queried.
So you would do:
my_button = button('retopoplz',ann='Select a Mesh to Retopologize', bgc=[.15,.15,.15],l='START RETOPOLOGY')
TextToMakeLive=textField(ann='Mesh Selected', bgc=[.2,0,0],edit=0,tx='NONE')
button(my_button, e=True, c=windows.Callback(Retopo, TextToMakeLive))
You were along the right thought chain when you suggested lambda. Pymel's Callback can be more advantageous over lambda here. Check out the docs: http://download.autodesk.com/global/docs/maya2014/zh_cn/PyMel/generated/classes/pymel.core.windows/pymel.core.windows.Callback.html

Python 3 How do I temporarily disable tracing variables

I am using Python 3.4.1 on windows, if that should help.
Q1: How do I temporarily disable tracing variables
I have a variable field with at trace, and I would like to temporarily disable the trace so that I can change the value of the field without triggering the call to the trace function.
Does it make sense?
And it might be that I am doing it all wrong (and I join a snippet of code):
I have a drop down list that shows a list of items I can choose from.
I have a second drop down list that shows, for each of the items in the first drop down menu, a list of "sub items", which of course must be updated when I change the first drop down menu.
Q2: The question is, how do I "repack" the second drop down menu when the first one is changed?
Here is the code:
import tkinter as tk
WORKINGWINDOWWIDTH = 800 # Width for the working window
WORKINGWINDOWHEIGHT = 800 # Height for the working window
root = tk.Tk()
w = tk.Canvas(root, width=WORKINGWINDOWWIDTH - 10, height=WORKINGWINDOWHEIGHT - 10, bg="darkred")
def display_parameters(*args):
print("args: {0}, and I have the following option: {1}".format(args, functionChoiceVar.get()))
if functionChoiceVar.get() == "Option 1":
print("I picked the first one...")
print("How do I repack the presets?")
elif functionChoiceVar.get() == "Option 2":
print("I picked the second one...")
return
def display_options(*args):
print("args: {0}, and I have the following suboption: {1}".format(args, presetChoiceVar.get()))
return
functionChoiceVar = tk.StringVar(root)
functionChoices = ['Option 1', 'Option 2']
functionOption = tk.OptionMenu(root, functionChoiceVar, *functionChoices)
functionOption.pack(side='left', padx=10, pady=10)
functionOption.place(x= 10, y=10)
functionChoiceVar.set('Option 1')
functionChoiceVar.trace("w", display_parameters)
presetChoiceVar = tk.StringVar(root)
presetChoices11 = ['Suboption 11', 'Suboption 12', 'Suboption 13', 'Suboption 14','Suboption 15']
presetChoices12 = ['Suboption 21', 'Suboption 22', 'Suboption 23', 'Suboption 24','Suboption 25']
presetOption = tk.OptionMenu(root, presetChoiceVar, *presetChoices11)
presetOption.pack(side='left', padx=10, pady=10)
presetOption.place(x= 100, y=10)
presetChoiceVar.set('Suboption 11')
presetChoiceVar.trace("w", display_options)
When you set a trace, tkinter will return an id which you can use to later remove the trace with the .trace_vdelete() method. To restart the trace, simply do what you did the first time.
An easy way to keep track of the trace id is to store it as an attribute right to the instance of a StringVar.
For example:
functionChoiceVar.trace_id = functionChoiceVar.trace("w", display_parameters)
...
functionChoiceVar.trace_vdelete("w", functionChoiceVar.trace_id)
(by the way, unrelated to the question that was asked, calling .pack() and then immediately calling .place() serves no purpose. You can remove the call to .pack() because it gets negated by the call to .place() )
A1: how to remove tracing temporarily & return it afterwards
aWriteTracerID = presetChoiceVar.trace( "w", aWriteHANDLER ) # SAVE <<aTracer>> ID#
# setup code continues
presetChoiceVar.trace_vdelete( "w", aWriteTracerID ) # REMOVE <<aTracer>>
# code
# that needs the write-tracer
# to have been removed
# ...
aWriteTracerID = presetChoiceVar.trace( "w", aWriteHANDLER ) # SET AGAIN
I ran into this myself while creating a new class which inherits from a base class taking a variable as an input keyword argument. So I developed the below routine which attempts to recover a named callback function from the globals() based on the fact that the Trace ID is nothing more than some numerical digits prepended to the original callback function name. I expect that those digits have some meaning, but I was not able to ascertain them. Here's the routine:
def get_observer_callback(id):
func = None
funcname = id.lstrip('0123456789')
if funcname != '<lambda>' and funcname in globals():
func = globals().get(funcname)
if type(func) is not type(lambda:()):
func = None
return func
While testing, a trace_id was 50360848my_callback and the callback function was identified as
<function my_callback at 0x03064370>
Interestingly, 50360848 from the wrapper is hex 0x03007210, which is in the same ballpark as hex 0x03064370 from the function description. I couldn't find a hard relationship between the two beyond that...
I would describe the above routine as a kludge, but maybe, given the Tk implementation, the above kludge is sufficient for named functions. Clearly it is not helpful for lambda functions. I expect that within Tk there's a registration table which holds all the desired information, including references to the function objects. Ideally a call which returns the function object from within the Tk internal table would be best. Any input is welcome.
The easiest way to remove a trace would be to say:
idx = 0 # This is the index of the first trace on tk_variable.
tk_variable.trace_remove(*tk_variable.trace_info()[idx])
To add the trace back, you can say:
tk_variable.trace_add('write', your_function)
To check if a variable is traced, you can say:
if tk_variable.trace_info():
# do something
pass

How to recognize a button from its command?

I have created a list of entries in a for-loop. All entries are stored in a list so I can just obtain all of the inputs later:
inputs = [e.get() for e in self.entries]
However, I have also created a button next to each entry in the for-loop (so they each call the same function). How can I make it so that it recognizes which button belongs to which row/entry? Is there something I can do with event?
row = 0
self.entries = []
self.comments = []
for n in names:
e = Entry(self.top, bd = 5)
e.insert(0, n)
e.grid(column = 1, row = self.row, sticky = 'NSWE', padx = 5, pady = 5)
self.entries.append(e)
self.comments += [""]
commentButton = Button(self.top, text = "comment", command = self.commentSelected)
commentButton.grid(column = 3, row = self.row, sticky = 'NSWE', padx = 5, pady = 5)
self.row = self.row + 1
Yes -- use Callback Shims ( Currying Functions )
( courtesy Russell Owen )
I find I often wish to pass extra data to a callback function, in addition that that normally given. For instance the Button widget sends no arguments to its command callback, but I may want to use one callback function to handle multiple buttons, in which case I need to know which button was pressed.
The way to handle this is to define the callback function just before you pass it to the widget and include any extra information that you require. Unfortunately, like most languages, Python doesn't handle the mixing of early binding (information known when the function is defined) and late binding (informtation known when the function is called) particularly well. I personally find the easiest and cleanest solution is:
Write my callback function to take all desired data as arguments.
Use a callback shim class to create a callable object that stores my function and the extra arguments and does the right thing when called. In other words, it calls my function with the saved data plus the data that the caller supplies.
I hope the example given below makes this clearer.
The callback shim I use is RO.Alg.GenericCallback, which is available in my RO package. A simplified version that does not handle keyword arguments is given in the example below. All shim code is based on a python recipe by Scott David Daniels, who calls this "currying a function" (a term that is probably more common than "callback shim").
#!/usr/local/bin/Python
""" Example showing use of a callback shim"""
import Tkinter
def doButton(buttonName):
""" My desired callback.
I'll need a callback shim
because Button command callbacks receive no arguments.
"""
print buttonName, "pressed"
class SimpleCallback:
""" Create a callback shim.
Based on code by Scott David Daniels
(which also handles keyword arguments).
"""
def __init__(self, callback, *firstArgs):
self.__callback = callback
self.__firstArgs = firstArgs
def __call__(self, *args):
return self.__callback (*(self.__firstArgs + args))
root = Tkinter.Tk()
buttonNames = ( "Button 1", "Button 2", "Button 3" )
for name in buttonNames:
callback = SimpleCallback( doButton, name )
Tkinter.Button( root, text = name, command = callback ).pack()
root.mainloop()
You can also use lambda:
from tkinter import *
def bla(b):
...
root = Tk()
buttons = []
for i in range(...):
button = Button(root)
button.configure(command=lambda b=button: bla(b)) # Make sure the Button object already exists
buttons.append(button)
button.pack()
root.mainloop()
As far as I see, you can't create the buttons in a single list comprehension now, but it is simpler and more readable than a class imho.

Combined Event Handling of widgets in TKinter

I am making a GUI Program in Tkinter and am running into problems.What I want to do is draw 2 checkboxes and a button. According to the user input next steps should take place. A part of my code has been shown below :-
CheckVar1 = IntVar()
CheckVar2 = IntVar()
self.C1 = Checkbutton(root, text = "C Classifier", variable = CheckVar1, onvalue = 1, offvalue = 0, height=5,width = 20).grid(row=4)
self.C2 = Checkbutton(root, text = "GClassifier", variable = CheckVar2, onvalue = 1, offvalue = 0, height=5, width = 20).grid(row=5)
self.proceed1 = Button(root,text = "\n Proceed",command = self.proceed(CheckVar1.get(),CheckVar2.get())).grid(row=6)
# where proceed prints the combined values of 2 checkboxes
The error that I am getting is typical ie a default value of both the selected checkboxes gets printed up and then there is no further input. The error that I get is NullType Object is not callable.
I searched on the net and I think the answer is related to lambda events or curry.
Please help ..
You're passing the value of self.proceed(CheckVar1.get(),CheckVar2.get()) to the Button constructor, but presumably what you want is for command to be set to a function which will call self.proceed(CheckVar1.get(),CheckVar2.get()) and return a new, possibly different value every time the button is pressed. You can fix that with a lambda, or by wrapping the call in a short callback function. For example, replace the last line with:
def callback():
return self.proceed(CheckVar1.get(), CheckVar2.get())
self.proceed1 = Button(root, text="\n Proceed", command=callback).grid(row=6)
This is pretty typical Tkinter. Remember: when you see a variable called command in Tkinter, it's looking for a function, not a value.
EDIT: to be clear: you're getting 'NullType Object is not callable' because you've set command to equal the return value of a single call to self.proceed (that's the NullType Object). self.proceed is a function, but its return value is not. What you need is to set command to be a function which calls self.proceed.
Like Peter Milley said, the command option needs a reference to a function (ie: give it a function name (ie: no parenthesis). Don't try to "inline" something, create a special function. Your code will be easier to understand and to maintain.

Categories

Resources