Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

after import tkmacosx, tk.Menu.delete() throw exception:argument of type 'NoneType' is not iterable #12

Closed
aeroju opened this issue Feb 4, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@aeroju
Copy link

aeroju commented Feb 4, 2021

Exception throw from:
tkinter/init.py", line 2923, in delete
if 'command' in self.entryconfig(i):

After dig into tkmacosx, in tkmacosx.variables, function:_colorvar_patch_configure, line 66~67 could be moved after line 70.

After doing that, this exception was gone and all works.

The reason is when call tk.Misc._configure, if cnf==None, it will return all cnf items( line 1472, tkinter.init.py )

@Saadmairaj
Copy link
Owner

Can you please provide complete trackback error and the situation caused it?

@Saadmairaj Saadmairaj added the bug Something isn't working label Feb 5, 2021
@aeroju
Copy link
Author

aeroju commented Feb 5, 2021

Can you please provide complete trackback error and the situation caused it?

Here is the sample code:

import tkinter as tk
import tkmacosx


class MyApp():
    def __init__(self):
        self.root = tk.Tk()
        self.root.configure(background='white')

        def clear_searchs():
            while (_menu.index(tk.END) is not None):
                i = _menu.index(tk.END)
                _menu.delete(i)

        tkmacosx.Button(self.root, text='search',
                        padx=10, pady=5,
                        borderless=1, borderwidth=0,
                        command=lambda: print('aaa'),
                        bg='green', fg='blue').pack(side='top', pady=(10, 0))

        tkmacosx.Button(self.root, text='clear',
                        padx=10, pady=5,
                        borderless=1, borderwidth=0,
                        command=clear_searchs,
                        bg='green', fg='blue').pack(side='top', pady=(10, 0))

        mbutton = tk.Menubutton(
            self.root, text='recent search', relief=tk.RAISED)
        _menu = tk.Menu(mbutton, tearoff=0, fg='black')
        mbutton.menu = _menu
        mbutton['menu'] = _menu
        mbutton.pack(side='top', padx=(0, 10))

        def do_search(item):
            print(item)

        _menu.add_command(label='search item1',
                          command=lambda: do_search('item1'))
        _menu.add_command(label='search item2',
                          command=lambda: do_search('item2'))


app = MyApp()
app.root.mainloop()

And after click button "clear", the complete trackback was:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/init.py", line 1705, in call
      return self.func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tkmacosx/basewidget.py", line 1094, in cmd
      self.cnf['command']()
  File "/Users/owl/workspace/test_tkmac.py", line 11, in clear_searchs
      _menu.delete(i)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/init.py", line 2923, in delete
      if 'command' in self.entryconfig(i):
TypeError: argument of type 'NoneType' is not iterable

@Saadmairaj
Copy link
Owner

I've fixed the error in the last commit c67ed8c, Though I haven't released it on pypi yet so you can force reinstall tkmacosx from the GitHub with the following command

pip install --upgrade --force-reinstall git+https://github.com/Saadmairaj/tkmacosx

@aeroju
Copy link
Author

aeroju commented Feb 6, 2021

Thanks for quick fix, however, there is another problem, here is the sample code:

import tkinter as tk
import tkmacosx
class MyApp():
    def __init__(self):
        self.root = tk.Tk()
        self.root.configure(background='white')

        self.canvas = tk.Canvas(self.root
                                     , borderwidth=0
                                     , highlightthickness=0
                                     , background='white')
        self.viewPort = tk.Frame(self.canvas,
                                      background='white')
        self.vsb = tk.Scrollbar(self.root, orient="vertical",
                                 command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)

        self.vsb.pack(side="right", fill="y")
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas_window = self.canvas.create_window((0, 0), window=self.viewPort, anchor="nw",
                                                       tags="self.viewPort")
        self.viewPort.bind("<Configure>",
                           self.onFrameConfigure)
        self.canvas.bind("<Configure>",
                         self.onCanvasConfigure)
        self.viewPort.bind("<MouseWheel>", self.onMouseWheel)
        self.canvas.bind("<MouseWheel>", self.onMouseWheel)

        self.onFrameConfigure(None)

        def clear_searchs():
            while (_menu.index(tk.END) is not None):
                i=_menu.index(tk.END)
                _menu.delete(i)
        tkmacosx.Button(self.viewPort, text='search',
                        padx=10, pady=5,
                        borderless=1, borderwidth=0,
                        command=lambda :print('aaa'),
                        bg='green', fg='blue').pack(side='top',pady=(10,0))
        tkmacosx.Button(self.viewPort, text='clear',
                        padx=10, pady=5,
                        borderless=1, borderwidth=0,
                        command=clear_searchs,
                        bg='green', fg='blue').pack(side='top',pady=(10,0))
        mbutton=tk.Menubutton(self.viewPort, text='recent search', relief=tk.RAISED)
        _menu = tk.Menu(mbutton,tearoff=0,fg='black')
        mbutton.menu=_menu
        mbutton['menu']=_menu
        mbutton.pack(side='top',padx=(0,10))
        def do_search(item):
            print(item)
        _menu.add_command(label='search item1',command=lambda : do_search('item1'))
        _menu.add_command(label='search item2',command=lambda : do_search('item2'))

    def onFrameConfigure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox(
            "all"))

    def onCanvasConfigure(self, event):
        canvas_width = event.width
        self.canvas.itemconfig(self.canvas_window,
                               width=canvas_width)
        self.onRedraw()

    def onMouseWheel(self,event):
        IS_WINDOWS=False
        if(IS_WINDOWS):
            self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
        else:
            if(event.delta<0 and self.vsb.get()[0]>0
                or event.delta>0 and self.vsb.get()[1]<1):
                self.canvas.yview_scroll(event.delta, "units")

    def onRedraw(self):
        pass


app = MyApp()
app.root.mainloop()

And the traceback when run:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/owl/workspace/test_tkmac.py", line 63, in onCanvasConfigure
    width=canvas_width)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2578, in itemconfigure
    return self._configure(('itemconfigure', tagOrId), cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tkmacosx/variables.py", line 75, in _patch
    tmp_dict = cnf if i in cnf else kw
TypeError: argument of type 'NoneType' is not iterable

@Saadmairaj Saadmairaj reopened this Feb 6, 2021
@Saadmairaj
Copy link
Owner

TypeError: argument of type 'NoneType' is not iterable

The above error has been fixed, use the same command to force reinstall tkmacosx from GitHub.

pip install --upgrade --force-reinstall git+https://github.com/Saadmairaj/tkmacosx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants