Python: Type error while using exec for creating TKINTER Checkbutton












-1















I want to create a checkbox using a loop(using dropbox files_list_folder('').entries) and get the text & status if it is checked, but it is showing TypeError and the problem is i need to assign var to get status of the checkbutton:



for ii,j in zip(range(len(files_list)),range(1,len(files_list)+1)):
exec("var%i=StringVar()"%j)
exec("ch%i = Checkbutton(text=files_list[ii],variable=var%i)"%j%j)
exec("ch%i.grid(row=ii, column=0, sticky=W)"%j)









share|improve this question























  • What's the TypeError you're seeing?

    – kingJulian
    Nov 13 '18 at 10:23











  • TypeError: not enough arguments for format string

    – bala bharath
    Nov 13 '18 at 10:33






  • 1





    You should never use exec like this, it makes your code nearly impossible to read and debug. Store your widgets in a dictionary or list instead. see How do I create a variable number of variables

    – Bryan Oakley
    Nov 13 '18 at 11:33













  • yes i understand, but whenever it is checked i need its text, thats y i need a var=StringVar() to be asigned to every checkbutton i create

    – bala bharath
    Nov 13 '18 at 11:58











  • I think it's because you're using % as a percent character in your format string. I believe you need to use %% instead.

    – kingJulian
    Nov 13 '18 at 12:07
















-1















I want to create a checkbox using a loop(using dropbox files_list_folder('').entries) and get the text & status if it is checked, but it is showing TypeError and the problem is i need to assign var to get status of the checkbutton:



for ii,j in zip(range(len(files_list)),range(1,len(files_list)+1)):
exec("var%i=StringVar()"%j)
exec("ch%i = Checkbutton(text=files_list[ii],variable=var%i)"%j%j)
exec("ch%i.grid(row=ii, column=0, sticky=W)"%j)









share|improve this question























  • What's the TypeError you're seeing?

    – kingJulian
    Nov 13 '18 at 10:23











  • TypeError: not enough arguments for format string

    – bala bharath
    Nov 13 '18 at 10:33






  • 1





    You should never use exec like this, it makes your code nearly impossible to read and debug. Store your widgets in a dictionary or list instead. see How do I create a variable number of variables

    – Bryan Oakley
    Nov 13 '18 at 11:33













  • yes i understand, but whenever it is checked i need its text, thats y i need a var=StringVar() to be asigned to every checkbutton i create

    – bala bharath
    Nov 13 '18 at 11:58











  • I think it's because you're using % as a percent character in your format string. I believe you need to use %% instead.

    – kingJulian
    Nov 13 '18 at 12:07














-1












-1








-1


1






I want to create a checkbox using a loop(using dropbox files_list_folder('').entries) and get the text & status if it is checked, but it is showing TypeError and the problem is i need to assign var to get status of the checkbutton:



for ii,j in zip(range(len(files_list)),range(1,len(files_list)+1)):
exec("var%i=StringVar()"%j)
exec("ch%i = Checkbutton(text=files_list[ii],variable=var%i)"%j%j)
exec("ch%i.grid(row=ii, column=0, sticky=W)"%j)









share|improve this question














I want to create a checkbox using a loop(using dropbox files_list_folder('').entries) and get the text & status if it is checked, but it is showing TypeError and the problem is i need to assign var to get status of the checkbutton:



for ii,j in zip(range(len(files_list)),range(1,len(files_list)+1)):
exec("var%i=StringVar()"%j)
exec("ch%i = Checkbutton(text=files_list[ii],variable=var%i)"%j%j)
exec("ch%i.grid(row=ii, column=0, sticky=W)"%j)






python-3.x tkinter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 9:52









bala bharathbala bharath

1415




1415













  • What's the TypeError you're seeing?

    – kingJulian
    Nov 13 '18 at 10:23











  • TypeError: not enough arguments for format string

    – bala bharath
    Nov 13 '18 at 10:33






  • 1





    You should never use exec like this, it makes your code nearly impossible to read and debug. Store your widgets in a dictionary or list instead. see How do I create a variable number of variables

    – Bryan Oakley
    Nov 13 '18 at 11:33













  • yes i understand, but whenever it is checked i need its text, thats y i need a var=StringVar() to be asigned to every checkbutton i create

    – bala bharath
    Nov 13 '18 at 11:58











  • I think it's because you're using % as a percent character in your format string. I believe you need to use %% instead.

    – kingJulian
    Nov 13 '18 at 12:07



















  • What's the TypeError you're seeing?

    – kingJulian
    Nov 13 '18 at 10:23











  • TypeError: not enough arguments for format string

    – bala bharath
    Nov 13 '18 at 10:33






  • 1





    You should never use exec like this, it makes your code nearly impossible to read and debug. Store your widgets in a dictionary or list instead. see How do I create a variable number of variables

    – Bryan Oakley
    Nov 13 '18 at 11:33













  • yes i understand, but whenever it is checked i need its text, thats y i need a var=StringVar() to be asigned to every checkbutton i create

    – bala bharath
    Nov 13 '18 at 11:58











  • I think it's because you're using % as a percent character in your format string. I believe you need to use %% instead.

    – kingJulian
    Nov 13 '18 at 12:07

















What's the TypeError you're seeing?

– kingJulian
Nov 13 '18 at 10:23





What's the TypeError you're seeing?

– kingJulian
Nov 13 '18 at 10:23













TypeError: not enough arguments for format string

– bala bharath
Nov 13 '18 at 10:33





TypeError: not enough arguments for format string

– bala bharath
Nov 13 '18 at 10:33




1




1





You should never use exec like this, it makes your code nearly impossible to read and debug. Store your widgets in a dictionary or list instead. see How do I create a variable number of variables

– Bryan Oakley
Nov 13 '18 at 11:33







You should never use exec like this, it makes your code nearly impossible to read and debug. Store your widgets in a dictionary or list instead. see How do I create a variable number of variables

– Bryan Oakley
Nov 13 '18 at 11:33















yes i understand, but whenever it is checked i need its text, thats y i need a var=StringVar() to be asigned to every checkbutton i create

– bala bharath
Nov 13 '18 at 11:58





yes i understand, but whenever it is checked i need its text, thats y i need a var=StringVar() to be asigned to every checkbutton i create

– bala bharath
Nov 13 '18 at 11:58













I think it's because you're using % as a percent character in your format string. I believe you need to use %% instead.

– kingJulian
Nov 13 '18 at 12:07





I think it's because you're using % as a percent character in your format string. I believe you need to use %% instead.

– kingJulian
Nov 13 '18 at 12:07












1 Answer
1






active

oldest

votes


















0














You should avoid the use of exec and eval whenever possible. These are very unsafe to someone who does not fully understand what they can do. Both have a severe risk of code injection.



You can use list to get results of each Checkbutton selection. We can store the StringVar() in a list and then call that value when button is checked or unchecked.



import tkinter as tk


root = tk.Tk()

value_list = ['One', 'Two', 'Three', 'Four']
var_list =

def print_results_from_selection(i):
print("{}: {}".format(value_list[i], var_list[i].get()))

def generate_buttons():
for i in range(len(value_list)):
var_list.append(tk.StringVar())
var_list[-1].set(0)
tk.Checkbutton(root, text=value_list[i], variable=var_list[-1],
command=lambda i=i: print_results_from_selection(i),
onvalue=1, offvalue=0).grid(row=i, column=0, sticky="w")

generate_buttons()
root.mainloop()





share|improve this answer
























  • Thanks you so much ! now its working like a charm

    – bala bharath
    Nov 13 '18 at 15:59











  • @balabharath glad to help. Be sure to select the check button next to my answer if it solved your problem. This will let everyone know you have got a solution to your question.

    – Mike - SMT
    Nov 13 '18 at 16:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278224%2fpython-type-error-while-using-exec-for-creating-tkinter-checkbutton%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You should avoid the use of exec and eval whenever possible. These are very unsafe to someone who does not fully understand what they can do. Both have a severe risk of code injection.



You can use list to get results of each Checkbutton selection. We can store the StringVar() in a list and then call that value when button is checked or unchecked.



import tkinter as tk


root = tk.Tk()

value_list = ['One', 'Two', 'Three', 'Four']
var_list =

def print_results_from_selection(i):
print("{}: {}".format(value_list[i], var_list[i].get()))

def generate_buttons():
for i in range(len(value_list)):
var_list.append(tk.StringVar())
var_list[-1].set(0)
tk.Checkbutton(root, text=value_list[i], variable=var_list[-1],
command=lambda i=i: print_results_from_selection(i),
onvalue=1, offvalue=0).grid(row=i, column=0, sticky="w")

generate_buttons()
root.mainloop()





share|improve this answer
























  • Thanks you so much ! now its working like a charm

    – bala bharath
    Nov 13 '18 at 15:59











  • @balabharath glad to help. Be sure to select the check button next to my answer if it solved your problem. This will let everyone know you have got a solution to your question.

    – Mike - SMT
    Nov 13 '18 at 16:04
















0














You should avoid the use of exec and eval whenever possible. These are very unsafe to someone who does not fully understand what they can do. Both have a severe risk of code injection.



You can use list to get results of each Checkbutton selection. We can store the StringVar() in a list and then call that value when button is checked or unchecked.



import tkinter as tk


root = tk.Tk()

value_list = ['One', 'Two', 'Three', 'Four']
var_list =

def print_results_from_selection(i):
print("{}: {}".format(value_list[i], var_list[i].get()))

def generate_buttons():
for i in range(len(value_list)):
var_list.append(tk.StringVar())
var_list[-1].set(0)
tk.Checkbutton(root, text=value_list[i], variable=var_list[-1],
command=lambda i=i: print_results_from_selection(i),
onvalue=1, offvalue=0).grid(row=i, column=0, sticky="w")

generate_buttons()
root.mainloop()





share|improve this answer
























  • Thanks you so much ! now its working like a charm

    – bala bharath
    Nov 13 '18 at 15:59











  • @balabharath glad to help. Be sure to select the check button next to my answer if it solved your problem. This will let everyone know you have got a solution to your question.

    – Mike - SMT
    Nov 13 '18 at 16:04














0












0








0







You should avoid the use of exec and eval whenever possible. These are very unsafe to someone who does not fully understand what they can do. Both have a severe risk of code injection.



You can use list to get results of each Checkbutton selection. We can store the StringVar() in a list and then call that value when button is checked or unchecked.



import tkinter as tk


root = tk.Tk()

value_list = ['One', 'Two', 'Three', 'Four']
var_list =

def print_results_from_selection(i):
print("{}: {}".format(value_list[i], var_list[i].get()))

def generate_buttons():
for i in range(len(value_list)):
var_list.append(tk.StringVar())
var_list[-1].set(0)
tk.Checkbutton(root, text=value_list[i], variable=var_list[-1],
command=lambda i=i: print_results_from_selection(i),
onvalue=1, offvalue=0).grid(row=i, column=0, sticky="w")

generate_buttons()
root.mainloop()





share|improve this answer













You should avoid the use of exec and eval whenever possible. These are very unsafe to someone who does not fully understand what they can do. Both have a severe risk of code injection.



You can use list to get results of each Checkbutton selection. We can store the StringVar() in a list and then call that value when button is checked or unchecked.



import tkinter as tk


root = tk.Tk()

value_list = ['One', 'Two', 'Three', 'Four']
var_list =

def print_results_from_selection(i):
print("{}: {}".format(value_list[i], var_list[i].get()))

def generate_buttons():
for i in range(len(value_list)):
var_list.append(tk.StringVar())
var_list[-1].set(0)
tk.Checkbutton(root, text=value_list[i], variable=var_list[-1],
command=lambda i=i: print_results_from_selection(i),
onvalue=1, offvalue=0).grid(row=i, column=0, sticky="w")

generate_buttons()
root.mainloop()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 13:55









Mike - SMTMike - SMT

9,39921234




9,39921234













  • Thanks you so much ! now its working like a charm

    – bala bharath
    Nov 13 '18 at 15:59











  • @balabharath glad to help. Be sure to select the check button next to my answer if it solved your problem. This will let everyone know you have got a solution to your question.

    – Mike - SMT
    Nov 13 '18 at 16:04



















  • Thanks you so much ! now its working like a charm

    – bala bharath
    Nov 13 '18 at 15:59











  • @balabharath glad to help. Be sure to select the check button next to my answer if it solved your problem. This will let everyone know you have got a solution to your question.

    – Mike - SMT
    Nov 13 '18 at 16:04

















Thanks you so much ! now its working like a charm

– bala bharath
Nov 13 '18 at 15:59





Thanks you so much ! now its working like a charm

– bala bharath
Nov 13 '18 at 15:59













@balabharath glad to help. Be sure to select the check button next to my answer if it solved your problem. This will let everyone know you have got a solution to your question.

– Mike - SMT
Nov 13 '18 at 16:04





@balabharath glad to help. Be sure to select the check button next to my answer if it solved your problem. This will let everyone know you have got a solution to your question.

– Mike - SMT
Nov 13 '18 at 16:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278224%2fpython-type-error-while-using-exec-for-creating-tkinter-checkbutton%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues