0

I want to check what option the dropdown menu is set to. The program check the day and the dropdown menu option and sets a string accordingly. Right now the code looks like this. Is there a way I can check the value of the dropdown menu?


class FrameStart1(tkinter.Frame):
    image = PhotoImage(file='C:/Users/plarkin2020334/Pictures/DQ_Logocp.png')
    def __init__(self, parent, *args, **kwargs):
        tkinter.Frame.__init__(self, parent, *args, **kwargs)
        Label(self, image=self.image).place(relx=0, rely=0, anchor=NW)
        Label(self, text="Enter any additional Instructiuons for the day:", background="#3f49e5").place(relx=.0, rely=.45)
        self.info = tkinter.StringVar()
        self.entry = tkinter.Entry(self, textvariable=self.info).place(relx=.0, rely=.51)
        Button(self, text="Add to Todays List", command=self.add_list).place(relx=.24, rely=.51)
        tkvar = StringVar()
        self.choices = {'Open', 'Day', 'Close'}
        self.opt = OptionMenu(self, tkvar, *self.choices, ).place(relx=.0008 , rely=.572)

    def add_list(self):
        global Day
        if date.today().weekday() == 2 and self.opt == 'Open':
            Day = "List_Wednesday_Morn.txt"
        with open(Day, "r+") as file:
            for line in file:
                if not line.strip():
                    continue
                else:
                    file.write("\n" + self.info.get())
martineau
  • 119,623
  • 25
  • 170
  • 301
Pat Larkin
  • 29
  • 1
  • 3
  • Does this answer your question? [Tkinter bringing chosen option from optionMenu into a variable for further use](https://stackoverflow.com/questions/52862893/tkinter-bringing-chosen-option-from-optionmenu-into-a-variable-for-further-use) – stovfl Dec 18 '19 at 16:59
  • 1
    To find out which choice is currently selected in an `OptionMenu` widget, the `get()` method on the associated control variable will return that choice as a string. Note that `{'Open', 'Day', 'Close'}` is not a `list` of strings, it's a `set` of strings. – martineau Dec 18 '19 at 17:05
  • use `self.` in `self.tkvar = StringVar()` and then you can check selected option in other method using `self.tkvar.get()` – furas Dec 18 '19 at 17:10
  • 1
    BTW: `self.opt = OptionMenu().place()` assigns `None` to `self.opt` because` `place()`/`grid()`/`pack()` returns `None`. If you need `self.opt` then you have to do it in two steps `self.opt = OptionMenu()` and `self.opt.place()`. The same problem can be with `self.entry` if you would like to use `self.entry.get()` – furas Dec 18 '19 at 17:13

0 Answers0