2

so, I'm now in a personal project (just to try myself) with python + tkinter. It is a encrypter, which means that it gets a piece of text and encrypts it using some famous cyphers (like the numerical cypher, the caesar's cypher, and others).
Now, I wanted to give the user the option to save the text he encrypted, and also the encrypted text generated by the program. For that, I created two checkbutton on the program's menu: one for "save text" and the other for "save encrypted text". My question is, I tried to attach a function as it's command option, so, I guess it should run that function when the option is clicked. But it isn't happening.
I'll just explain what the funcitions shouls do before passing the code.
They should prompt a question, asking the user if he/she really wants to create a text file with the text and the encrypted text (This isn't a database, it's just something for the user to be able to read later the text's he/she encrypted and the encrypted version, if he/she wants to). So, to the code:

encryptermenu = Menu(menubar, tearoff=0)
encryptermenu.add_checkbutton(label="Save Text", variable=v, command=saveText)
encryptermenu.add_checkbutton(label="Save Encrypted Text", variable=w, command=saveEncryptedText)
menubar.add_cascade(label="Encrypter", menu=encryptermenu)

The checkbutton options, and now the functions:

def saveText():
    sdtst = messagebox.askyesno(title="Save Text", message="A .txt file will be created at the same directory as this program to save the text you decided to encrypt. Is it ok?")

def saveEncryptedText():
    sdtset = messagebox.askyesno(title="Save Encrypted Text", message="A .txt file will be created at the same directory as this program to save the encrypted text generated by this program. Is it ok?")

Should the checkbutton really run the function on-click or am I just making confusion? Anyway, hope someone will help me.

2
  • Is it all code? Are there any error messages? Code seems correct for me. Except one thing: why do you use add_checkbutton instead of add_command? Commented May 4, 2013 at 9:20
  • No error messages, the only problem is that nothing happens. The checkbutton option is to create an menu option that is toggable, so it shows a tick when it is active, and doesn't when is inactive. I decided to use that because I would like to leave a tick whenever the text or the encrypted text were being saved, so that the user would know. Commented May 4, 2013 at 19:17

2 Answers 2

2

Checkbutton Menu can definitely call a function. That is what the checkbutton option is there for :)

I generally use 2 functions to handle a call from the checkbutton menu.

first create that menu and assign it to a boolean var

yesno = BooleanVar(root)
mymenu.add_checkbutton(label="Do this ?", variable=yesno, command=mytoggle)

then you need 2 functions: 1) one callback to toggle 2) one to handle yes

def mytoggle(event=None):
    val = yesno.get()
    if val:
        dosomething()
    else:
        somethingelse()
Sign up to request clarification or add additional context in comments.

2 Comments

Still, when I click the check button option nothing happens. It was supposed to prompt a message box. I've created a function for calling the messagebox and a function for when the option is clicked and, yet, nothing happens when I click it. Also, it shows no error message or anything, it just doesn't happen.
@AugustoQ : you can always check where in your code you are getting stuck by printing out at each control line. For example within def mytoggle, after val = yesno.get(), you can print the value of val 'print val' to see what value the check button is passing to the variable 'val'
1

To answer your specific question, yes, the function will be called when you click on the checkbutton.

You should be calling add_command rather than add_checkbutton on the menu. Using a checkbutton to call a function from a menu is highly unusual and will likely confuse your users.

3 Comments

The thing is, what I was going for was those buttons that get a tick when active, so the user could know when it was active and when it is not. But as I understand from you answer, i believe the checkbutton on the menu can't call a function, is that right? Anyway, thanks for the help.
Well, I decided not to prompt the user anymore, so my checkbuttons don't need to call a function.
@AugustoQ: no, you misunderstood. I clearly said the function should be called even if you use a checkbutton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.