Fairly new to tkinter and python I was wondering how to achieve a button that would act like this :
Click on button drops down a list (so that's a combobox)
Each line of the list has a checkbox.
Finally if a checkbox is clicked run a function, or (even better) once combobox is no more dropped run a function with items checked as args.
UPDATE
The button/menuButton will have to act like a filter. When menu is dropped down user can uncheck multiple options (without the menu to disappear each time an item is clicked) he don't want. Therefore it's really important to be able to see checkboxes so as the user know which options are currently active.
I finally used the idea of Bryan by creating a top level frame. Here is what I have :
There is no widget to do what you want. You'll have to create a toplevel window with a bunch of checkbuttons. You can then trigger the appearance with a normal button.
I don't think the OptionMenu is intended to hold anything but strings. It sounds like you want the functionality of a Listbox, which has options to allow for multiple selections, get all selected items, and so on.
This gives you an OptionMenu with checkboxes in the contained Menu. Check whichever items you like, then right-click in the tkinter window to print the values of the checkboxes to the console.
from tkinter import *
master = Tk()
var = StringVar(master)
var.set("Check")
w = OptionMenu(master, variable = var, value="options:")
w.pack()
first = BooleanVar()
second = BooleanVar()
third = BooleanVar()
w['menu'].add_checkbutton(label="First", onvalue=True,
offvalue=False, variable=first)
w['menu'].add_checkbutton(label="Second", onvalue=True,
offvalue=False, variable=second)
w['menu'].add_checkbutton(label="Third", onvalue=1,
offvalue=False, variable=third)
master.bind('<Button-3>', lambda x: print("First:", first.get(), " Second:",
second.get(), " - Third:", third.get()))
mainloop()
See also this.
Related
I have a tkinter GUI which includes an Entry Widget (tk.Entry). At the moment this displays a numeric value, which can be incremented up/down by a couple of tkinter Buttons. In addition to incrementing the value displayed in the Entry Widget, whenever the Button is clicked it executes a command which updates the appropriate setting on a physical instrument with the new value from the Entry Widget.
I am wondering if it is possible at all to also have the option that if the user types a number into the Entry Widget to overwrite what is already there, that this can execute the same command as clicking the up/down buttons? For example, if the value is set to 10, and the user enters 100, then the command is executed to update the real instrument to 100.
I tried to add the code command=mycommandname to the Entry Widget (e.g. input_wavelength = tk.Entry(pwr_mtr_ctrl_frame, relief=tk.GROOVE, font=( "Ariel", 11), justify='center', validate='key', vcmd=pwr_vcmd, command=mycommandname) but get the error "unknown option "-command""
I guess this means that the Entry Widget does not have the option to execute a function, like a Button widget does? Are there ways to implement this somehow?
def print_something(*args):
print('hello world')
root = TK()
entry1 = Entry(root)
entry1.bind("<Return>", print_something)
# you can use other keys and replace it with "<Return>". EX: "f"
# by default, this function will pass an unknown argument to your function.
# thus, you have to give your function a parameter; in this case, we will use *args
root.mainloop()
According to what you described you would be better "served" by using a Spinbox.
This being said you need to bind a function to the <Return> event captured by the Entry if you want to execute something when the user enters something:
Here is how to do it:
input_wavelength = tk.Entry(pwr_mtr_ctrl_frame, relief=tk.GROOVE, font=( "Ariel", 11), justify='center', validate='key', vcmd=pwr_vcmd)
input_wavelength.bind("<Return>",mycommandname)
If mycommandname is also used as a command you need to declare it like this:
def mycommandname(event=None):
...
So I am using xlrd module to read names of excel files of a workbook that user selects. Now this comes up in form of a list, which I am able to put in popup menu drop down(able to select on name at a time) now I want to
So far I am able to use the drop down menu of tkinter with dynamic input but able to select just one name at a time, now I want to select multiple items at a time by adding checkbox to the same. Can anyone help.. Here is sample code on which you anyone may please try adding checkbox and select multiple names at a time.
from tkinter import *
root = Tk()
a = [] #creates a list to store the job names in
var = StringVar() # creates a stringvar to store the value of options
for i in range(20): # fills list with nonsense jobs for troubleshooting
a.append("Job Name "+str(i))
var.set(a[0]) # sets the default option of options
options = OptionMenu(root, var, *a)
# creates an option menu populated with every element of the list
button = Button(root, text="Ok", command=lambda:print(var.get()))
# prints the current value of options
options.pack()
button.pack()
The above code shows a drop down with single select able item, I just want multiple selection functionality enabled by adding checkbox in front of every option
Use the MenuButton feature in tkinter. It has a method called Menu. To this method you could add as many check buttons as you want. This will support multiselect dropdown option. Give a variable name to each checkbutton and access its state through the get command.
I want users to be able to select and deselect the radio button.
When i run my program it automatically selects itself. I want users to be able to click it on and off. How can i do this
thanks :)
You simply need to set the variable assigned to the variable attribute of the Radiobutton to a value not assigned in your Radiobutton, like the below:
from tkinter import *
root = Tk()
var = StringVar()
var.set(None)
radio = Radiobutton(root, text="Option1", variable=var, value=True)
radio.pack()
root.mainloop()
I am a new python user. I'm used to programing on matlab.
I've trying to make a simple GUI with Tkinter pack, but I'm having some problems with that. I had already read and searched what i want but I couldn't develop it.
What I'm trying to do is to make a listbox and when I choose one (or more) options the index be returned (and stored) as a variable (array or vector) that could be used to indexing another array.
The best result I got was a listbox where the index were printed, but not stored as a variable (at least it hasn't been shows in the variables list)
I'm using spyder (anaconda).
I tryied a lot of codes and I don't have this anymore.
Sorry for the dumb question. I guess I still thinking in a Matlab way to write
To keep this application simple, your best option is to get the listbox selection when you want to do something with it:
from tkinter import Tk, Listbox, MULTIPLE, END, Button
def doStuff():
selected = lb.curselection()
if selected: # only do stuff if user made a selection
print(selected)
for index in selected:
print(lb.get(index)) # how you get the value of the selection from a listbox
def clear(lb):
lb.select_clear(0, END) # unselect all
root = Tk()
lb = Listbox(root, selectmode=MULTIPLE) # create Listbox
for n in range(5): lb.insert(END, n) # put nums 0-4 in listbox
lb.pack() # put listbox on window
# notice no parentheses on the function name doStuff
doStuffBtn = Button(root, text='Do Stuff', command=doStuff)
doStuffBtn.pack()
# if you need to add parameters to a function call in the button, use lambda like this
clearBtn = Button(root, text='Clear', command=lambda: clear(lb))
clearBtn.pack()
root.mainloop()
I've also added a button to clear the listbox selection because you cannot unselect items by default.
First, import tkinter, then, create the listbox. Then, you can use curselection to get the contents of the listbox.
import tkinter as tk
root = tk.Tk() #creates the window
myListbox = tk.Listbox(root, select=multiple) #allows you to select multiple things
contentsOfMyListbox = myListbox.curselection(myListbox) #stores selected stuff in tuple
See the documentation here.
I have a function which will display features of an image. Once it got displayed, I want to refresh it for the next image.
For instance if I have a window like this
Once I click on the Refresh Button the window should be like this
Initially every feature values are set to zero.
How to achieve this? I have tried with grid_forget() it will not give me what I wanted. I don't want to clear the window, I just want to clear the entries in each of the Entry box, and I want to clear the Label too, so that I can select another image
Thanks in advance!
If you have a lot of Entry widgets, like in your example, you can use the winfo_children() method to get a list of all the children widgets of an object. Then, you can iterate through this list to reset and configure the widgets it contains:
root = Tk()
def clear_all():
for widget in root.winfo_children(): # get all children of root
if widget.winfo_class() == 'Entry': # if the class is Entry
widget.delete(0,END) # reset its value
elif widget.winfo_class() == 'Label': # get Label widgets
widget.config(bg='green') # and change them as well
# set up misc. widgets
for i in range(5):
Entry(root).pack()
Label(root, height=10, width=100, bg='red').pack()
Button(root, text='Refresh', command=clear_all).pack()
mainloop()
All you need to do is set the entry widgets to whatever default values you want:
# if you do not use the textvariable attribute:
self.entry1.delete(0, "end")
self.entry1.insert(0, "0")
self.entry2.delete(0, "end")
self.entry2.insert(0, "0.0")
# if you DO use the textvariable attribute:
self.entry1var.set("0")
self.entry2var.set("0.0")
This is all documented on the Entry man page, and in most tkinter tutorials. A good place to start when researching how to use individual widgets is effbot.org. For example, here is their page on the Entry widget: http://effbot.org/tkinterbook/entry.htm