So I am looking to create a mini GUI, where I need to configure certain vales. The first time I ran this tool it worked, however, when I run it again without a fresh kernel ran values are stuck at their default values. I think it could be a memory leak issue, but I am not sure how to check.
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 26 08:55:59 2024
@author: khourym
"""
import tkinter as tk
from tkinter import ttk
# Function to create a top-level dropdown GUI and return dictionary on submit
def create_dropdowns(options, root):
# This dictionary will hold the labels as keys and dropdown values as values
result = {}
def validate_float_input(char, text):
""" Validate the input for the float entry. """
print(char)
if (
all(char in "0123456789.-" for char in text) and # all characters are valid
"-" not in text[1:] and # "-" is the first character or not present
text.count(".") <= 1): # only 0 or 1 periods
return True
else:
return False
def on_submit():
# Update the result dictionary with the current dropdown selections
for option, var in variables.items():
print(var.get())
result[option] = var.get() # Get the current value of the dropdown
# Get the value of the float entry
sv_value = float_entry_var.get()
top.destroy() # Close the top-level window
return result, sv_value
# Create a top-level window
top = tk.Toplevel(root, background="cyan")
top.title("Top-Level Dropdown GUI")
# Create a frame to hold all dropdowns
infoFrame = tk.Frame(top,background="cyan")
infoFrame.pack()
cTile = tk.Label(infoFrame, text="Please configure the Control Logic states inn voltage with the VIH values and enter the nominal nvoltage for the supply voltage(VCC) all in volts",background="cyan")
cTile.pack(expand=False, fill='both')
frame = tk.Frame(top, background="cyan")
frame.pack()
# Create a dictionary to hold variables associated with dropdown values
variables = {}
# Iterate over the array of strings and create a label and dropdown for each
for option in options:
# Create a label for the string
dropdown_label = tk.Label(frame, text="Enter the value for the control logic " + option, background="cyan")
dropdown_label.pack(anchor="w") # Create a variable to hold the selected value for each option
var = tk.StringVar(value="X") # default value is "X"
float_entry = tk.Entry(frame, textvariable=var)
float_entry.pack(anchor="w") # Store the variable in the dictionary with the option label as the key
variables[option] = var # Create a label and entry box for the float value
# Create a label and entry box for the float value
float_label = tk.Label(frame, text="Enter The supply voltage VIH value", background="cyan")
float_label.pack(anchor="w")
float_entry_var = tk.StringVar(value="5.0") # default value is "0.0"
# Register the validation function
vcmd = (root.register(validate_float_input), "%S", "%P")
float_entry = tk.Entry(frame, textvariable=float_entry_var, validate="key", validatecommand=vcmd)
float_entry.pack(anchor="w")
# Create a submit button
submit_button = tk.Button(top, text="Submit", command=on_submit, background="skyblue")
submit_button.pack(pady=10)
# Start the Tkinter main loop
root.wait_window(top)
# Return the result dictionary with the label-value pairs and the float value
return result, float(float_entry_var.get())
if __name__ == "__main__":
# Example usage
options_array = ["Option 1", "Option 2", "Option 3"]
root = tk.Tk()
root.withdraw() # Hide the root window, only show the top-level window
selected_values = create_dropdowns(options_array,root)
print(selected_values) # This will print the dictionary after you close the top-level GUI'''
with a fresh kernel: ({'Option 1': '4', 'Option 2': '4', 'Option 3': '7'}, 5.5)
not a fresh kernel: ({'Option 1': 'X', 'Option 2': 'X', 'Option 3': 'X'}, 5.0)
I am running this through spyder and the “restart kernel” is opening a new console through the app which in turn restarts kernel.
2