tkinter return names of dynamic checkboxes
I'm trying to write my first script in python & tkinter.
I blocked and I'm lost when I need to get in variable the name of each checkboxes selected when I click on the button Validate.
Checkboxes are dynamic from text files. Sample file:
item1
item2
...
item100
A screen of the GUI:
Here's my code:
(In #
code is what I tried without success.)
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here the function which display checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
path = '/root/liste/*.txt'
files=glob.glob(path)
count = 0
#var = dict()
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
#var[item]=IntVar()
#cb = Checkbutton(frame1, text=item.rstrip(), variable=var[item], command=self.cb)
##Here all checkboxes generated dynamically
cb = Checkbutton(frame1, text=item.rstrip())
cb.grid(row=count//10, column=count%10)
#cb.pack()
count += 1
#btn1 = Button(self, text='Validate', font=("Arial", 12), command=self.cb)
btn1 = Button(self, text='Validate', font=("Arial", 12))
btn1.pack(side=RIGHT, padx=5)
def cb(self):
print("variable is", self.var.get())
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
python checkbox tkinter
add a comment |
I'm trying to write my first script in python & tkinter.
I blocked and I'm lost when I need to get in variable the name of each checkboxes selected when I click on the button Validate.
Checkboxes are dynamic from text files. Sample file:
item1
item2
...
item100
A screen of the GUI:
Here's my code:
(In #
code is what I tried without success.)
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here the function which display checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
path = '/root/liste/*.txt'
files=glob.glob(path)
count = 0
#var = dict()
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
#var[item]=IntVar()
#cb = Checkbutton(frame1, text=item.rstrip(), variable=var[item], command=self.cb)
##Here all checkboxes generated dynamically
cb = Checkbutton(frame1, text=item.rstrip())
cb.grid(row=count//10, column=count%10)
#cb.pack()
count += 1
#btn1 = Button(self, text='Validate', font=("Arial", 12), command=self.cb)
btn1 = Button(self, text='Validate', font=("Arial", 12))
btn1.pack(side=RIGHT, padx=5)
def cb(self):
print("variable is", self.var.get())
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
python checkbox tkinter
add a comment |
I'm trying to write my first script in python & tkinter.
I blocked and I'm lost when I need to get in variable the name of each checkboxes selected when I click on the button Validate.
Checkboxes are dynamic from text files. Sample file:
item1
item2
...
item100
A screen of the GUI:
Here's my code:
(In #
code is what I tried without success.)
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here the function which display checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
path = '/root/liste/*.txt'
files=glob.glob(path)
count = 0
#var = dict()
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
#var[item]=IntVar()
#cb = Checkbutton(frame1, text=item.rstrip(), variable=var[item], command=self.cb)
##Here all checkboxes generated dynamically
cb = Checkbutton(frame1, text=item.rstrip())
cb.grid(row=count//10, column=count%10)
#cb.pack()
count += 1
#btn1 = Button(self, text='Validate', font=("Arial", 12), command=self.cb)
btn1 = Button(self, text='Validate', font=("Arial", 12))
btn1.pack(side=RIGHT, padx=5)
def cb(self):
print("variable is", self.var.get())
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
python checkbox tkinter
I'm trying to write my first script in python & tkinter.
I blocked and I'm lost when I need to get in variable the name of each checkboxes selected when I click on the button Validate.
Checkboxes are dynamic from text files. Sample file:
item1
item2
...
item100
A screen of the GUI:
Here's my code:
(In #
code is what I tried without success.)
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here the function which display checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
path = '/root/liste/*.txt'
files=glob.glob(path)
count = 0
#var = dict()
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
#var[item]=IntVar()
#cb = Checkbutton(frame1, text=item.rstrip(), variable=var[item], command=self.cb)
##Here all checkboxes generated dynamically
cb = Checkbutton(frame1, text=item.rstrip())
cb.grid(row=count//10, column=count%10)
#cb.pack()
count += 1
#btn1 = Button(self, text='Validate', font=("Arial", 12), command=self.cb)
btn1 = Button(self, text='Validate', font=("Arial", 12))
btn1.pack(side=RIGHT, padx=5)
def cb(self):
print("variable is", self.var.get())
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
python checkbox tkinter
python checkbox tkinter
edited Nov 15 '18 at 23:38
martineau
69.4k1092186
69.4k1092186
asked Nov 15 '18 at 21:33
Indi59Indi59
526
526
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I can't figure out everything your code is trying to do with respect to handling multiple files, but the following shows a cleaned-up and functioning version of it showing a way to keep track which Checkbutton
s have been selected by the user.
Most of the important changes were to the onDisplay()
method—although I also changed the name of the def cb(self):
method you had to def validate(self):
to match the Button
name (because I found calling it cb
confusing since that's also the name of a local variable in onDisplay()
).
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here is the function which displays checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
# path = '/root/liste/*.txt'
path = './root_liste1.txt' # changed for my testing.
files=glob.glob(path)
self.var = dict()
count = 0
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
item = item.rstrip()
status = BooleanVar()
self.var[item] = status
cb = Checkbutton(frame1, text=item, variable=status)
cb.grid(row=count//10, column=count%10)
count += 1
btn1 = Button(self, text='Validate', font=("Arial", 12),
command=self.validate)
btn1.pack(side=RIGHT, padx=5)
def validate(self): # btn1 callback
print('checked items:')
for item, status in self.var.items():
if status.get(): # Checked?
print(' ', item)
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
1
Many thanks Martineau,for explanations. It's exactly what i searched
– Indi59
Nov 16 '18 at 13:26
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328208%2ftkinter-return-names-of-dynamic-checkboxes%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
I can't figure out everything your code is trying to do with respect to handling multiple files, but the following shows a cleaned-up and functioning version of it showing a way to keep track which Checkbutton
s have been selected by the user.
Most of the important changes were to the onDisplay()
method—although I also changed the name of the def cb(self):
method you had to def validate(self):
to match the Button
name (because I found calling it cb
confusing since that's also the name of a local variable in onDisplay()
).
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here is the function which displays checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
# path = '/root/liste/*.txt'
path = './root_liste1.txt' # changed for my testing.
files=glob.glob(path)
self.var = dict()
count = 0
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
item = item.rstrip()
status = BooleanVar()
self.var[item] = status
cb = Checkbutton(frame1, text=item, variable=status)
cb.grid(row=count//10, column=count%10)
count += 1
btn1 = Button(self, text='Validate', font=("Arial", 12),
command=self.validate)
btn1.pack(side=RIGHT, padx=5)
def validate(self): # btn1 callback
print('checked items:')
for item, status in self.var.items():
if status.get(): # Checked?
print(' ', item)
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
1
Many thanks Martineau,for explanations. It's exactly what i searched
– Indi59
Nov 16 '18 at 13:26
add a comment |
I can't figure out everything your code is trying to do with respect to handling multiple files, but the following shows a cleaned-up and functioning version of it showing a way to keep track which Checkbutton
s have been selected by the user.
Most of the important changes were to the onDisplay()
method—although I also changed the name of the def cb(self):
method you had to def validate(self):
to match the Button
name (because I found calling it cb
confusing since that's also the name of a local variable in onDisplay()
).
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here is the function which displays checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
# path = '/root/liste/*.txt'
path = './root_liste1.txt' # changed for my testing.
files=glob.glob(path)
self.var = dict()
count = 0
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
item = item.rstrip()
status = BooleanVar()
self.var[item] = status
cb = Checkbutton(frame1, text=item, variable=status)
cb.grid(row=count//10, column=count%10)
count += 1
btn1 = Button(self, text='Validate', font=("Arial", 12),
command=self.validate)
btn1.pack(side=RIGHT, padx=5)
def validate(self): # btn1 callback
print('checked items:')
for item, status in self.var.items():
if status.get(): # Checked?
print(' ', item)
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
1
Many thanks Martineau,for explanations. It's exactly what i searched
– Indi59
Nov 16 '18 at 13:26
add a comment |
I can't figure out everything your code is trying to do with respect to handling multiple files, but the following shows a cleaned-up and functioning version of it showing a way to keep track which Checkbutton
s have been selected by the user.
Most of the important changes were to the onDisplay()
method—although I also changed the name of the def cb(self):
method you had to def validate(self):
to match the Button
name (because I found calling it cb
confusing since that's also the name of a local variable in onDisplay()
).
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here is the function which displays checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
# path = '/root/liste/*.txt'
path = './root_liste1.txt' # changed for my testing.
files=glob.glob(path)
self.var = dict()
count = 0
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
item = item.rstrip()
status = BooleanVar()
self.var[item] = status
cb = Checkbutton(frame1, text=item, variable=status)
cb.grid(row=count//10, column=count%10)
count += 1
btn1 = Button(self, text='Validate', font=("Arial", 12),
command=self.validate)
btn1.pack(side=RIGHT, padx=5)
def validate(self): # btn1 callback
print('checked items:')
for item, status in self.var.items():
if status.get(): # Checked?
print(' ', item)
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
I can't figure out everything your code is trying to do with respect to handling multiple files, but the following shows a cleaned-up and functioning version of it showing a way to keep track which Checkbutton
s have been selected by the user.
Most of the important changes were to the onDisplay()
method—although I also changed the name of the def cb(self):
method you had to def validate(self):
to match the Button
name (because I found calling it cb
confusing since that's also the name of a local variable in onDisplay()
).
from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("My Menu")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="lst1", command=self.onDisplay)
submenu.add_command(label="lst2")
submenu.add_command(label="lst3")
fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
## Here is the function which displays checkboxes
def onDisplay(self):
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=BOTH)
lbl1 = Label(frame1, text="Choice", width=6)
# path = '/root/liste/*.txt'
path = './root_liste1.txt' # changed for my testing.
files=glob.glob(path)
self.var = dict()
count = 0
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
item = item.rstrip()
status = BooleanVar()
self.var[item] = status
cb = Checkbutton(frame1, text=item, variable=status)
cb.grid(row=count//10, column=count%10)
count += 1
btn1 = Button(self, text='Validate', font=("Arial", 12),
command=self.validate)
btn1.pack(side=RIGHT, padx=5)
def validate(self): # btn1 callback
print('checked items:')
for item, status in self.var.items():
if status.get(): # Checked?
print(' ', item)
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("800x550+300+300")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
edited Nov 16 '18 at 16:47
answered Nov 15 '18 at 23:11
martineaumartineau
69.4k1092186
69.4k1092186
1
Many thanks Martineau,for explanations. It's exactly what i searched
– Indi59
Nov 16 '18 at 13:26
add a comment |
1
Many thanks Martineau,for explanations. It's exactly what i searched
– Indi59
Nov 16 '18 at 13:26
1
1
Many thanks Martineau,for explanations. It's exactly what i searched
– Indi59
Nov 16 '18 at 13:26
Many thanks Martineau,for explanations. It's exactly what i searched
– Indi59
Nov 16 '18 at 13:26
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328208%2ftkinter-return-names-of-dynamic-checkboxes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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