Can't make Radiobuttons pre-selected with tkinter

Question:

sadly I didn’t found any solution to my problem I realy hope U guys can maybe help me.
I have a main.py and, for test purposes, a test.py.
In the main I only create the window for my programm and the widgets come from the test.py, the thing is my radio Buttons wont be pre-selected. When i run my test.py as own code, with own window etc., it works. What makes me very confused is the fact that in an other code from me I don’t have that problem at everything works as intended. All very the same.

So here is my main.py code

import tkinter as tk
from test import test

window = tk.Tk()

test(window)

window.mainloop()

and this is the code from the test.py

import tkinter as tk
from tkinter import ttk, ***messagebox***

def test(fenster):
rb_variable = tk.StringVar()
rb_variable.set('exakt')

txt_selection = tk.StringVar()
txt_selection.set('Text 1')
rbtxt1 = ttk.Radiobutton(fenster, text='Text 1', variable=txt_selection, value='Text 1')
rbtxt2 = ttk.Radiobutton(fenster, text='Text 2', variable=txt_selection, value='Text 2')
rbtxt3 = ttk.Radiobutton(fenster, text='Text 3', variable=txt_selection, value='Text 3')

Fuer den Titel
titel_selection = tk.StringVar()
titel_selection.set('Hinweis')
rbti1 = ttk.Radiobutton(fenster, text='Hinweis', variable=titel_selection, value='Hinweis')
rbti2 = ttk.Radiobutton(fenster, text='Warnung', variable=titel_selection, value='Warnung')
rbti3 = ttk.Radiobutton(fenster, text='Fehler', variable=titel_selection, value='Fehler')

Fuer die Art
msg_selection = tk.StringVar()
msg_selection.set('showinfo')
rbmsg1 = ttk.Radiobutton(fenster, text='Information', variable=msg_selection, value='showinfo')
rbmsg2 = ttk.Radiobutton(fenster, text='Warnung', variable=msg_selection, value='showwarning')
rbmsg3 = ttk.Radiobutton(fenster, text='Fehler', variable=msg_selection, value='showerror')

Die Radiobuttons (in reihe eins)
rbtxt1.grid(row=1, column=0, padx=15)
rbti1.grid(row=1, column=1, padx=15)
rbmsg1.grid(row=1, column=2, padx=15)

Die Radiobuttons (in reihe zwei)
rbtxt2.grid(row=2, column=0, padx=15)
rbti2.grid(row=2, column=1, padx=15)
rbmsg2.grid(row=2, column=2, padx=15)

Die Radiobuttons (in drei zwei)
rbtxt3.grid(row=3, column=0, padx=15)
rbti3.grid(row=3, column=1, padx=15)
rbmsg3.grid(row=3, column=2, padx=15)

***messagebox.showinfo('', '')***

I realy hope U have an idea

I already tried using tk.Radiobutton and ttk.Radiobutton – thing is, with tk.Radiobutton the Radiobuttons get selected only by hover over them – and they do not de-select each other on that either.

EDIT:
Okay, I placed a message box right after I placed all Radiobuttons, and when the messagebox show´s I see that the Radiobuttons i wish to be pre-selected are indeed selected. But jsut as i close the messagebox the (visible) selection disapears.

Asked By: Borilion

||

Answers:

After function test() returns, those local tkinter variables, e.g. txt_selection, will be garbage collected. So those radiobuttons will be deselected.

You need to save the references of those variables so that they will not be garbage collected after the function exits. One of the way is to use attributes of the main window:

test.py
def test(fenster):
    ...
    # use attribute of "fenster" to save the reference of the tkinter variable
    fenster.txt_selection = txt_selection = tk.StringVar()
    ...
    fenster.titel_selection = titel_selection = tk.StringVar()
    ...
    fenster.msg_selection = msg_selection = tk.StringVar()
    ...
Answered By: acw1668
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.