0

after updating the list a OptionMenu uses it does not automatically update. I tried fixing this by implementing

Options['values'] = options

but I only get the error: "_tkinter.TclError: unknown option "-values"

The problem is located in the "update()" function

Code:


from pynput import *
import tkinter as tk
from tkinter import ttk
import time, threading

#

count = 0

Selected = "none"

options = ["action 1"]

class Action():
    def __init__(self, assigned, action, cordX, cordY):
        self.assigned = assigned
        self.action = action
        self.cordX = cordX
        self.cordY = cordY

class Program(tk.Tk):
    def __init__(self, title, minSize):
        #Main Setup
        super().__init__()
        self.title (title)
        self.geometry(f'{minSize[0]}x{minSize[1]}')
        self.minsize(minSize[0],minSize[1])
        
        #Widgets
        self.menu = Menu(self)

        #run
        self.mainloop()

class Menu(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.place(x = 0, y = 0)
        
        self.create_widgets()

    def selected(event):
        global selected
        Textbox = Text(self, width=40, height=10)
        Textbox.grid(row=1,column=0)
        Selected = clicked.get()
        print(Selected)

    def ENTERINPUT():
        global selected
        f = open("Actions.txt", "a")
        f.write(selected, assigned, action, cordX, cordY)
        
    def create_widgets(self):
        global options
        
        clicked = tk.StringVar()
        clicked.set(options[0])
        
        ListOfActions = tk.OptionMenu(self, clicked, *options)
        ListOfActions.grid(row = 0, column = 1, sticky = 'nsew', pady=20)

        def update():                                    ## < THE PROBLEM
            global options, count
            count += 1
            options.append("action" + str(count))
            ListOfActions['values'] = options
            print(options)
            
        #create widgets
        button1 = ttk.Button(self, text = "Add Action", command=update)
        
        #create grid
        self.columnconfigure((0,1,2), weight = 1, uniform = 'a')
        self.rowconfigure((0,1,2,3,4), weight = 1, uniform = 'a')
            
        #place widgets  
        button1.grid(row = 0, column = 0, sticky = 'nsew', pady=20)
        
Program("Automation Program", (300,250))


I've looked and found other users who used the same code but didn't have errors. Maybe it's a version thing?

2 Answers2

0

If you study the code of the class OptionMenu, a Menu widget will be created inside the class and Menu.add_command() is used to create those menu items using the passed options (with the help of an internal class _setit()).

You can use same logic on adding new item to the option list as below:

def update():
    action = f"action {len(options)+1}"
    options.append(action)
    print(options)
    ListOfActions['menu'].add_command(label=action, command=tk._setit(clicked, action))
acw1668
  • 40,144
  • 5
  • 22
  • 34
-1

You mention a ComboBox but it appears you created an OptionMenu widget. The tk.OptionMenu and ttk.Combobox are separate widgets. Perhaps creating a Combobox instead of an OptionMenu is what you intended.

gesling
  • 67
  • 4
  • I'm so sorry, I don't know why I said combobox in the question. Is there still a way to update the OptionMenu? – Techo WinMC Jul 05 '23 at 17:59
  • I believe this may address the question: https://stackoverflow.com/questions/28412496/updating-optionmenu-from-list – gesling Jul 05 '23 at 21:43