Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter combobox - gracefully lose focus when click out of widget

I have a ComboBox written in Python Tkinter that makes the horrible system alert sound when you click off of it without selecting something.

For instance, when you hit the dropdown and select your item, it works fine. But if you hit the drop-down and then decide to click off, it will lose focus as expected, but it makes an alert sound. Can this be disabled in some way so it can gracefully lose focus without complaining? I'm on OSX 10.9 btw

UPDATE - Minimally working code that produces the alert.

from Tkconstants import *
import ttk
import Tkinter

class PyPrecursor():

    def __init__(self,root):
        self.root = root
        self.TabNotebook()

    def TabNotebook(self):
        self.main_notebook_frame = ttk.Notebook(self.root, name='main_notebook')
        self.main_notebook_frame.enable_traversal()
        self.OptionsF = ttk.Frame(self.main_notebook_frame, name='options')
        self.length_options_frame = ttk.LabelFrame(
        self.OptionsF, labelwidget=ttk.Label(font=('Arial', 15), text="Length Options:"))
        self.hcdr3_length_label = ttk.Label(self.length_options_frame, text="HCDR3 Length")
        self.HCDR3_Length = Tkinter.StringVar()
        self.hcdr3_length_combo = ttk.Combobox(
        self.length_options_frame, values=[i for i in xrange(16, 36)], 
             textvariable=self.HCDR3_Length)
        self.hcdr3_length_combo.current(0)
        self.length_options_frame.pack(side=TOP,fill=X,pady=5)
        self.hcdr3_length_label.pack(side=LEFT)
        self.hcdr3_length_combo.pack(side=LEFT,anchor=W)
        self.main_notebook_frame.pack(side=TOP,expand=1,fill=BOTH,padx=10,pady=10)
        self.main_notebook_frame.add(
            self.OptionsF, text="Input Options", underline=0, padding=2)
        self.main_notebook_frame.bind("<<NotebookTabChanged>>",self.update_)

    def update_(self,event):
        self.root.update()

def main():
    root = Tkinter.Tk()
    PyPrecursor(root)
    root.mainloop()
    root.update_idletasks()


if __name__ == '__main__':
    main()
like image 238
jwillis0720 Avatar asked Feb 20 '15 04:02

jwillis0720


People also ask

How to remove focus from entry widget Tkinter?

Setting focus to parent widget or to the root window removes focus from the target widget. Some widgets have takefocus option. Set takefocus to 0 to take your widget out of focus traversal (when user hits <Tab> key).

What does focus () do in tkinter?

Focus is used to refer to the widget or window which is currently accepting input. Widgets can be used to restrict the use of mouse movement, grab focus, and keystrokes out of the bounds. However, if we want to focus a widget so that it gets activated for input, then we can use focus.

What is the purpose of the focus method in a tkinter widget Python?

It focuses the widget and makes them active until the termination of the program.

Does tkinter have combobox?

The Tkinter Combobox is one of the UI widgets in the Tkinter library and it is used for to select the values in the drop-down list column also it is one of the combinations of Entry and drop-down widgets these drop-down entries are replaceable one and the user if suppose not select the value in the box it automatically ...


1 Answers

You might want to try this: self.hcdr3_length_combo.bell(displayof=1)

Not sure if it should be 1 or 0 though... If it doesn't work, maybe a containing widget throws the sound. Might want to apply it to the parent widget as well. I'm not familiar with python 2.7 and it doesn't throw a sound when I use python3 with slight modifications.

Usually when you can't find an option for a specific widget, you can find something in the general widget options. Just search "tkinter widget options" and you'll get some place like: https://effbot.org/tkinterbook/widget.htm

like image 191
PythonAmateur742 Avatar answered Sep 19 '22 20:09

PythonAmateur742