1

I'm using Radiobuttons in tkinter and I can select them fine but when the mouse hovers over one of the options it gets selected without me actually clicking the left button on the mouse.

from tkinter import *

p = Tk()

def select_months():
    new_window = Toplevel()
    new_window.resizable(width=FALSE, height=FALSE)

    days_label = Label(new_window,text="Select month")
    days_label.grid()

    ycolumn = 0
    xrow = 1

    list_of_months = ["January", "February ", "March ", "April "]
    list_of_months_variable = StringVar()

    # list_of_months_variable.set(None)

    for i in list_of_months:
        Radiobutton(new_window, text=i, variable=list_of_months_variable, value=str(i)).grid(padx=4, row=xrow, column=ycolumn)
        ycolumn += 1

        if ycolumn > 2:
            ycolumn = 0
            xrow += 1

    ok_button = Button(new_window, text="OK", command=new_window.destroy).grid()
        
select_month_label = Label(p,text="which month ? : ").pack(side="left")
select_month_button = Button(p, text="months...", command=select_months).pack()

p.mainloop()
Ghost Ops
  • 1,710
  • 2
  • 13
  • 23
alfaplt
  • 35
  • 3

1 Answers1

3

It's a funny issue, took a while to debug.
Basically the select_months() function finishes. But the window appears with all the placed buttons so it is harder to understand that the function has completed. What happens when a function completes? All the local variables to the function get garbage collected. This means that the reference to list_of_months_variable is lost too. That causes some issues with the widgets, can't tell exactly why this (select on hover) happens but the solution is pretty simple.

global list_of_months_variable

Add this to your select_months() function somewhere before defining the variable (usually at the top of function definition):

def select_months():
    global list_of_months_variable
    new_window = Toplevel()
    # the rest of the code ...

Also set value=0 for the StringVar:

list_of_months_variable = StringVar(value=0)

so that none of the Radiobuttons are pre-selected.


I would also suggest this:
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.

I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Don't have space around = if it is used as a part of keyword argument (func(arg='value')) but have space around = if it is used for assigning a value (variable = 'some value'). Have space around operators (+-/ etc.: value = x + y(except here value += x + y)). Have two blank lines around function and class declarations.

Matiiss
  • 5,970
  • 2
  • 12
  • 29