0

your textI am making a Custom Scientific Calculator in tkinter Python and I want a button to reset everything in the program to how it was when the program first started. It is heavily unfinished but I am doing it one step at a time and so far I have not been able to find out how to reset my OptionMenu. I am using VSCode with Python and Pylance extensions.

This is the code I have that is relevant to the problem and if you would like me to copy and paste the entire script I can.

#Creates the variable for the option menu 
opmOneDefault = StringVar(frm)
opmOneDefault.set("FORMULA")
opmOneVariable = tk.StringVar(value=opmOneDefault)

#This is where functions are defined
def make_formula_option_menu():
    formula_option_menu = OptionMenu(frm, opmOneVariable, opmOneDefault, 
                        "Moles of A to Grams of B",
                        "Moles of A to Moles of B",
                        "Grams of A to Grams of B"
                        )

    formula_option_menu.grid(column=0, row=8, sticky=SW)

def reset_all():
   opmOneVariable.set(opmOneDefault)

reset_button = ttk.Button(frm,
                        text="Reset",
                        command=reset_all)

reset_button.grid(column=0, row=4, sticky=SW)

formula_option_menu = OptionMenu(frm, opmOneDefault, 
                        "Moles of A to Grams of B",
                        "Moles of A to Moles of B",
                        "Grams of A to Grams of B"
                        )

#This is where the OptionMenus are told where to be
formula_option_menu.grid(column=0, row=8, sticky=SW)

I have Googled my problem but have not found a effective solution. I have tried the reset_all function with various different commands including: setvar(), setattr(), optionclear, and other similar ways but am stuck on this. I was expecting one of these to work but obviously they didn't reset the drop down box to how it was before I clicked and chose an option.

2 Answers2

1

Simply change opmOneDefault to a string instead of StringVar. Also make_formula_option_menu() has never been called and so it can be removed. Final you have not pass opmOneVariable to OptionMenu(...) in the main block.

Updated code:

#Creates the variable for the option menu
opmOneDefault = "FORMULA"  # changed to a string instead of StringVar
opmOneVariable = tk.StringVar(value=opmOneDefault)

def reset_all():
   opmOneVariable.set(opmOneDefault)

reset_button = ttk.Button(frm,
                          text="Reset",
                          command=reset_all)

reset_button.grid(column=0, row=4, sticky=tk.SW)

# added passing opmOneVariable
formula_option_menu = tk.OptionMenu(frm, opmOneVariable, opmOneDefault,
                        "Moles of A to Grams of B",
                        "Moles of A to Moles of B",
                        "Grams of A to Grams of B"
                        )

#This is where the OptionMenus are told where to be
formula_option_menu.grid(column=0, row=8, sticky=tk.SW)

It seems like you have both from tkinter import * and import tkinter as tk. It imports tkinter twice and wildcard import should be avoided. Use import tkinter as tk, then add prefix tk. to all tkinter constants and widget names.

acw1668
  • 40,144
  • 5
  • 22
  • 34
0

To restore the OptionMenu to its default value, you need to update the opmOneVariable variable with the default value. Currently, your reset_all() function is assigning the opmOneVariable to the opmOneDefault, but it won't reflect on the OptionMenu because the OptionMenu is linked to the opmOneVariable but doesn't directly refer to opmOneDefault.

To resolve this, you should update the opmOneVariable by assigning it back to the value retrieved from opmOneDefault.get() in the reset_all() function. Here's how you can modify your code:

import tkinter as tk
from tkinter import ttk, StringVar, OptionMenu, SW

# Your other code here

def reset_all():
    opmOneVariable.set(opmOneDefault.get())

# The rest of your code here

This way, when the "Reset" button is clicked, the opmOneVariable will be assigned the value retrieved from opmOneDefault.get(), effectively restoring the OptionMenu to the default value.

With this change, when you click the "Reset" button, it should restore the OptionMenu to the default value "FORMULA" that you set at the beginning of the program.