2

Is it possible to sort 0:5 to frame 1, and sort 5:10 to frame 2 using one list? Now it is clicking both radiobuttons in frame 1 and 2. I would like that only one can be selected at the time.

My code:

import tkinter as tk
from tkinter import *

List1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

List2 = List1[ 5: ]
List1 = List1[ :5 ]

window = Tk()
window.geometry( "300x300" )

x = tk.IntVar()
x.set( 'none' )

frame_1 = LabelFrame( window, text="Label Frame 1" )
frame_2 = LabelFrame( window, text="Label Frame 2" )
frame_1.grid( row=0, column=0, sticky='nsew' )
frame_2.grid( row=0, column=1, sticky='nsew' )

for index in range( len(List1) ):
    radiobutton = Radiobutton( frame_1, text = List1[index], variable = x, value = index)
    radiobutton.pack( anchor = 'nw' )

for index in range( len(List2) ):
    radiobutton = Radiobutton( frame_2, text = List2[index], variable = x, value = index )
    radiobutton.pack( anchor = 'nw' )

window.columnconfigure( (0, 1), weight = 1, uniform = 'a' )
window.rowconfigure( (0), weight = 1 )    
window.mainloop()

2 Answers 2

3

You set the values of those radiobuttons in left frame the same as those in right frame, so clicking one radiobutton on one frame will select corresponding one in another frame.

You need to assign unique value to each radiobutton instead by using the values of the list:

import tkinter as tk

List1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
half = len(List1) // 2

window = tk.Tk()
window.geometry('300x300')

frame1 = tk.LabelFrame(window, text='Label Frame 1')
frame2 = tk.LabelFrame(window, text='Label Frame 2')
frame1.grid(row=0, column=0, sticky='nsew')
frame2.grid(row=0, column=1, sticky='nsew')

x = tk.IntVar(value=-1)

for value in List1[:half]:
    tk.Radiobutton(frame1, text=value, variable=x, value=value).pack(anchor='nw')

for value in List1[half:]:
    tk.Radiobutton(frame2, text=value, variable=x, value=value).pack(anchor='nw')

window.rowconfigure(0, weight=1)
window.columnconfigure((0, 1), weight=1)

window.mainloop()

Note that wildcard import should be avoided.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think I've ever seen a variable named half used in this manner. Very clever! I makes the code just a little bit easier to understand.
2

The issue is that both sets of radio buttons are linked because they're using the same variable x. You need separate variables for each frame.

import tkinter as tk
from tkinter import *

List1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

List2 = List1[ 5: ]
List1 = List1[ :5 ]

window = Tk()
window.geometry( "300x300" )


x1 = tk.IntVar()  # For frame_1
x2 = tk.IntVar()  # For frame_2

x1.set( 'none' )
x2.set( 'none' )

frame_1 = LabelFrame( window, text="Label Frame 1" )
frame_2 = LabelFrame( window, text="Label Frame 2" )
frame_1.grid( row=0, column=0, sticky='nsew' )
frame_2.grid( row=0, column=1, sticky='nsew' )

for index in range( len(List1) ):
    radiobutton = Radiobutton( frame_1, text = List1[index], variable = x1, value = List1[index] )
    radiobutton.pack( anchor = 'nw' )

for index in range( len(List2) ):
    radiobutton = Radiobutton( frame_2, text = List2[index], variable = x2, value = List2[index] )
    radiobutton.pack( anchor = 'nw' )

window.columnconfigure( (0, 1), weight = 1, uniform = 'a' )
window.rowconfigure( (0), weight = 1 )
window.mainloop()

each frame now has its own independent radio button group, so selecting one won't affect the other.

1 Comment

But then there's gonna be two activated radiobuttons when I click something in the left frame en something in the right frame. I would like it so there can only be one activated at a time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.