How to make sure that a checkbox appears unchecked in Tkinter - python-2.7

I have a problem which looks like some kind of a bug. Sometimes it happens that a Checkbutton first appears checked when created and it seems like there is no apparent reason for this behaviour. I'm using external variables to get information about status of the checkbox and I'm sure I reset them right before the declaration. My declaration looks like this:
#item is a string
cbtn = Checkbutton(master, command=lambda method=item: fun(method))
You can see there's even no variable or state argument in the declaration, so I think this should always create an empty checkbox. Yet somehow, sometimes it appears checked at first, which is a problem because I'm creating a lock and then it looks opposite than it's supposed to.
So, is there a sureproof way to make it appear without a checkmark?

So, is there a sureproof way to make it appear without a checkmark?
Yes. Explicitly set the onvalue and offvalue attributes, associate a variable with the checkbutton, and explicitly set the value of the variable to the same as the offvalue.

Related

Use IF function to check if parameter is set without having to also create a condition

I want to do this: https://cloudonaut.io/optional-parameter-in-cloudformation/
Basically just want to use IF to check if a parameter was set at deploy time
But I really resent having to create this weird middle-man "condition" for this. It's very convoluted, not readable, and I'll need to do it for every param I add that I want this behaviour for, so it's not scalable either.
Is there any way to just set the default value of a param to False and use IF to see if it was set without creating this weird "condition" thing?
Unfortunately, this is not support. From docs Fn::If requires the first argument to be condition:
A reference to a condition in the Conditions section. Use the condition's name to reference it.

How does one bypass SyntaxError when parsing code?

I am using openpyxl to read an excel file that will have changing values over time. The following function will take string inputs from the excel sheets to make frames for Tkinter.
def make_new_frame(strng, frame_location, frame_name, frame_list):
if not(frame_name in frame_list):
frame_list.append(frame_name)
exec("global %s" %(frame_name)) in globals()
exec("%s = Frame(%s)"%(frame_name, frame_location))
.... etc.
The code itself is quite long but I think this is enough of a snapshot to address my problem.
Now this results in the following error while parsing:
SyntaxError: function 'make_new_frame' uses import * and bare exec, which are illegal because it is a nested function
Everything in the code I included parsed and executed just fine several times, but after I added a few more lines in later versions in this function, it keeps spitting out the above error before executing the code. The error references the third line in the function, (which, I repeat, has been cleared in the past).
I added "in globals()" as recommended in another SO post, so that solution is not working.
There is a solution online here that uses setattr, which I have no idea how to use to create a widget without eventually using exec.
I would really appreciate if someone could tell me how to bypass the error while parsing or provide an alternative means for a dynamically changing set of frame names.
Quick Note:
I am aware that setting a variable as global in python is generally warned against, but I am quite certain that it will serve useful for my code
Edit 1: I have no idea why this was downvoted. If I have done something incorrectly, please let me know what it is so I can avoid doing so in the future.
I think this is an X/Y problem. You are asking for help with solution Y instead of asking for help on problem X.
If your goal is to create an unknown number of Frame objects based on external data, you can store references to the frame in a list or dictionary without having to resort to using exec and dynamically created variable names.
exec is a perfectly fine function, but is one of those things that you should never use until you fully understand why you should never use it.
Here's how to solve your actual problem without using exec:
frames = {}
def make_new_frame(strng, frame_location, frame_name, frames):
if not(frame_name in frames):
frames[frame_name] = Frame(frame_location)
return frames[frame_name]
With that, you now have a dictionary (frames) that includes a reference for every new frame by name. If you had a frame named "foo", for example, you could configure and pack it like this:
frames["foo"].configure(background="red", ...)
frames["foo"].pack(...)
If preserving the order of the frames is important you can use an OrderedDict.

python tkinter radiobutton state

How do you get the state of a Button (clicked or unclicked)? This is a button state and not a question about variables set by the button. How do I tell if an object in a Frame is selected using i.winfo_class() to identify a Radiobutton.
i.e.
for i in a.winfo_children():
if i.winfo_class() == "Radiobutton":
i.get()
Radiobuttons have no get attribute so this will never work, but the logic is the same. I've looked through documentation without success.
The only solution is to get the value of the associated variable and compare it to the value of the radiobutton. If they are the same, that radiobutton is selected.
Found the solution. ttk.Radiobutton has more functionality than the tkinter.Radiobutton.
Please make note there there are major issues with all methods and values dereferences by Radiobuttons even now. Sometimes you get a pointer, sometimes you get a string and need to dereference yourself. This makes Radiobuttons a more difficult Widget to deal with than most of the others I've been working with. There are many documents on this and they are fixing this, but it is something to note.
For my purposes, tkinter.Radiobutton has no instate which means there is no simple way to check if it is clicked. I was getting pointer after pointer and dereferencing was far too tedious and complex to code for a dynamic gui.
The code I've ended up with is as follows:
for i in a.winfo_children():
if i.winfo_class() == "TRadiobutton" and i.instate(['selected']) is True:
return i.cget('value')
Also make note that TRadioButton string is used instead of RadioButton, the object type returned is not the same between ttk and tkinter implementations of Radiobutton.

Always display flagged messages in mutt

In mutt, I'd like to keep track of important messages which I need to answer, pretty much like I did in GMail before. The problem is that I tend to forget that I have such important messages, and so, never run the macro which makes flagged messages appear (in case they are too old to appear in the index). So, I'd like that the flagged messages always appear in the main index, no matter how old they are. This setting, along with the fact that the flagged messages appear in a different color (they already do), should be enough to prevent me to forgetting answering old - but important - messages. Is that possible ?
Thanks in advance!
The ordering of messages cannot be changed arbitrarily outside the scope of sort and sort_aux. However, you can limit the messages in your index to specific kinds of mail and bind everything neatly to macros, like so:
macro index .i "<limit>(~N|~F)<Enter>" "view new/flag"
macro index .a "<limit>~A<Enter>" "view all"
The first macro .i will limit messages in the index to only flagged (starred) and new ones, whereas the latter .a will remove any limit and show all messages, again.
You can automatically apply one of these views when entering a mail folder by utilizing a folder-hook.
folder-hook */INBOX push .i
Just alter the regular expression following the hook to whatever folder(s) you want to apply the macro to. Simply press .a at any point in the index to show all messages, again.
For further details consult the manpage, as it is fairly comprehensive.
That's a great hint and it works, I only had to change folder-hook to point to /var/mail/username (Linux INBOX).
I only wonder if the "push" (or maybe even the macro itself) could be made conditional, that is to work only if there are any new/flagged messages. As it is now, if there are no such messages, one gets empty list and has to manually execute ".a" macro.

Putting Controls at ToolBar- Stuck

I have been staring at this documentation for 5 hours now. I simply cant connect the steps. If you guys can enlighten me of the stuff.
Here is the site:
http://msdn.microsoft.com/EN-US/library/bb983718(VS.110).aspx
So my problem are the following:
-at number 5, it asked me to "Set these parameters as follows:", it didnt even mention anything about where? Where to implement the constructor, and why are we using CMFCToolbarComboBoxButton? when it already asked me at step 4 to derive a clas called CFindComboButton. Shouldnt I be making a contstructor for that one instead?
-at number 4(sorry about the non organized numbering of problems), what I did is use the add class (not the class wizard), and then I picked MFC Class. I then enter the supposedly CFindComboButton and Base class as CMFCToolBarComboBoxButton. Did I do something wrong on this one? Do I have to do anything for the ID ID_EDIT_FIND_COMBO?
-When I register the ID_EDIT_FIND_COMBO at the String Table, I dont exactly know what I did. Did I just register an id for future implementation? or is it something else?
-So I cant do step 5, I skipped to step 6. All it ask me is to look for the CreateCombo method athe the override section of properties at CFindComboButton. Well I can only find 3 override. None of them are CreateCombo method. Well from there, you can tell that I'm lost.
I'm a noob at mfc so you might wanna take that in consideration.
Even though your question is a bit jump-led up, let me try and answer so that it works for you.
Create two class - one derived from CComboBox (call it CFindComboBox) and another from CMFCToolBarComboBoxButton (call it CFindComboBoxButton). First class will implement the Combobox that will be shown when you click the drop down button in the toolbar. This drop down button is implemented by CFindComboBoxButton. Hope this is clear.
Now define the constructor for the CFindComboBoxButton as CFindComboBoxButton(UNIT nID, int nImage, DWORD dwStyles) using three parameters as explained below:
Command ID of the button which will be ID_EDIT_FIND_COMBO (or anything you want to define it as). This will get defined in the String Table. Just add a new entry in String Table with ID_EDIT_FIND_COMBO as ID and a placeholder string. Don't omit the string value else the ID will not get defined. The string value can be anything as it wont be used anywhere.
Second parameter will just be a call to CCommandManager::GetCmdImage(ID_EDIT_FIND). This will return the default image used to show the drop down for combobox. In case you want to use your own custom image you can create one and instead pass the ID of that.
Third parameter is the styles you want to use. They are defined at http://msdn.microsoft.com/EN-US/library/7h63bxbe(v=vs.110).aspx but you can use the default value (CBS_DROPDOWNLIST) to start with.
Override the CreateCombo method of CMFCToolBarComboBoxButton and add its implementation to CFindComboBoxButton. In this method create and return a pointer to CFindComboBox (CComboBox derived class).
I hope this clears all the confusion and you should be on your way to have a custom Combobox embedded inside a toolbar.
take a look at the VisualStudioDemo Sample:
http://msdn.microsoft.com/en-us/library/bb983983%28v=vs.90%29.aspx
you can find the CFindComboButton implementation there