How to keep the last selection of a Tkinter radio button selected?

Asked

Viewed 87 times

0

Good night! Is there any way to keep the last option clicked on radiobuttion buttons in python Tkinter lib selected? I would like, even when closing the program, when I opened it again, the option was selected. Thank you

import tkinter as tk
import serial
import time

ser = serial.Serial('COM4',9600, timeout=1)
time.sleep(2)

root = tk.Tk()
root.title("LED RGB CONTROL - RAMON BASILIO PROJECT")
root.geometry("600x120")
root.configure(background="#12AEFF")

v = tk.IntVar()
v.set(0)  # initializing the choice, i.e. Python

colors = [
    "LED_1",
    "LED_2",
    "LED 1/LED 2",
    "NONE"
]

def ShowChoice():
    print(v.get())
    if v.get() == 0:
        ser.write(b'0')

    if v.get() == 1:
        ser.write(b'1')

    if v.get() == 2:
        ser.write(b'2')

    if v.get() == 3:
        ser.write(b'3')    

tk.Label(root, 
         text="Choose what Led will turn on",
         background = "#12AEFF",
         padx = 20).pack(anchor=tk.W)

for val, color in enumerate(colors):
    tk.Radiobutton(root, 
                  text=color,
                  indicatoron = 0,
                  width = 20,
                  padx = 20, 
                  variable=v, 
                  command=ShowChoice,
                  value=val).pack(anchor=tk.W)
root.mainloop()

The basic code is this. My intention is to make a GUI to control an addressable LED tape that I will install on my pc. The LED tape in turn is connected to an Arduin. What I wanted was to keep the last choice fixed even after closing and opening the window.

  • The only way that comes to mind is to save a kind of configuration file. Every time you start, load it with the saved configs. Always paying attention to the possibilities of the user deleting the configuration file or is badly formatted.

  • It can even be known formats such as json or xml.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.