raymund
raymund

Reputation: 51

How to save tkinter OptionMenu & Entry widgets' contents to a file?

I am trying to save the user input from a optionmenu widget and entry widget on a file, the problem is the user input in entry widget is not being save properly. below is my code.

from tkinter import *                                                 

class ComputeTax:

    def __init__(self, master):

        self.compute_tax_frame = Frame(master)
        self.compute_tax_frame.pack()

        self.tax_status_label = Label(self.compute_tax_frame, text="input tax      status:")
        self.tax_status_label.grid(row=0, column=0, sticky=E)
        self.status_option = StringVar(self.compute_tax_frame)
        self.status_option.set("S")
        self.tax_status_option = OptionMenu(self.compute_tax_frame,
                                            self.status_option, "S", "M", "M1", "M2", "M3", "M4",
                                            command=self.save_ts)
        self.tax_status_option.grid(row=0, column=1, sticky=W)

        self.monthly_salary_label = Label(self.compute_tax_frame, text="input    monthly pay:")
        self.monthly_salary_label.grid(row=1, column=0, sticky=E)
        self.salary_input = StringVar(self.compute_tax_frame)
        self.salary_input.set("0.00")
        self.monthly_salary_input = Entry(self.compute_tax_frame,
                                          textvariable=self.salary_input)
        self.monthly_salary_input.grid(row=1, column=1)

        self.calculate_button = Button(self.compute_tax_frame, text="Save &     Calculate now!")
        self.calculate_button.grid(row=3, columnspan=2)

    def save_ts(self, value):

        tax = value
        pay = self.salary_input.get()

        with open("monthly_net_pay", "w") as file:
            file.write("{}\n".format(tax))
            file.write("{}\n".format(pay))
            file.close()

frame = Tk()
a_compute_class = ComputeTax(frame)
frame.title("Monthly Tax & Net Pay Computation")
frame.mainloop()

Upvotes: 1

Views: 870

Answers (1)

martineau
martineau

Reputation: 123501

I see a couple of problems with your code. The main one is that OptionMenu widgets don't have a command option/argument, so the command=self.save_ts doesn't accomplish anything (I'm a little surprised that no exception was raised from you attempting to specify it.)

The way to fix this is use the supply it when creating the calculate_button.

The second is that the save_ts() function expects a value argument, but one won't be passed to it. To correct that, first remove the parameter from the function def and instead get the needed value from the Stringvar widget you have named status_option.

Here's your code with the suggested fixes applied to it:

from tkinter import *

class ComputeTax:
    def __init__(self, master):
        self.compute_tax_frame = Frame(master)
        self.compute_tax_frame.pack()

        self.tax_status_label = Label(self.compute_tax_frame, 
                                      text="input tax status:")
        self.tax_status_label.grid(row=0, column=0, sticky=E)
        self.status_option = StringVar(self.compute_tax_frame)
        self.status_option.set("S")
        self.tax_status_option = OptionMenu(self.compute_tax_frame,
                                            self.status_option,
                                            "S", "M", "M1", "M2", "M3", "M4")
        self.tax_status_option.grid(row=0, column=1, sticky=W)

        self.monthly_salary_label = Label(self.compute_tax_frame,
                                          text="input monthly pay:")
        self.monthly_salary_label.grid(row=1, column=0, sticky=E)
        self.salary_input = StringVar(self.compute_tax_frame)
        self.salary_input.set("0.00")
        self.monthly_salary_input = Entry(self.compute_tax_frame,
                                          textvariable=self.salary_input)
        self.monthly_salary_input.grid(row=1, column=1)

        self.calculate_button = Button(self.compute_tax_frame,
                                       text="Save & Calculate now!",
                                       command=self.save_ts)
        self.calculate_button.grid(row=3, columnspan=2)

    def save_ts(self):
        tax = self.status_option.get()
        pay = self.salary_input.get()
        with open("monthly_net_pay", "w") as file:
            file.write("{}\n".format(tax))
            file.write("{}\n".format(pay))
#            file.close()  # automatic when using `with`

frame = Tk()
a_compute_class = ComputeTax(frame)
frame.title("Monthly Tax & Net Pay Computation")
frame.mainloop()

Upvotes: 1

Related Questions