TkinterWeb icon indicating copy to clipboard operation
TkinterWeb copied to clipboard

HtmlFrame error on m1 macbook pro: incompatible architecture, and missing files

Open cuteufo opened this issue 2 years ago • 11 comments

Source code snippet:

# gui2.py
import tkinter as tk
from tkinterweb import HtmlFrame

class GUI:
    def __init__(self) -> None:
        self.root = tk.Tk()
        self.root.geometry("815x416")

        self.info_html = HtmlFrame(self.root, width=1, height=1, horizontal_scrollbar="auto",
                                   messages_enabled=False)
        self.info_html.grid(row=0, column=0)

        self.root.mainloop()

if __name__ == "__main__":
    GUI()

Error:

$ python gui2.py
Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/bindings.py", line 82, in __init__
    load_tkhtml(master, folder)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/utilities.py", line 671, in load_tkhtml
    master.tk.eval("package require Tkhtml")
_tkinter.TclError: dlopen(/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/tkhtml/Darwin/64-bit/Tkhtml30.dylib, 0x0006): tried: '/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/tkhtml/Darwin/64-bit/Tkhtml30.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/Tkhtml30.dylib' (no such file), '/usr/lib/Tkhtml30.dylib' (no such file)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/cuteufo/Projects/gui2.py", line 17, in <module>
    tradematch_GUI()
  File "/Users/cuteufo/Projects/gui2.py", line 9, in __init__
    self.info_html = HtmlFrame(self.root, width=1, height=1, horizontal_scrollbar="auto",
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/htmlwidgets.py", line 30, in __init__
    self.html = html = TkinterWeb(self, message_func, HtmlFrame)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/bindings.py", line 85, in __init__
    load_tkhtml(master, folder, force=True)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/utilities.py", line 671, in load_tkhtml
    master.tk.eval("package require Tkhtml")
_tkinter.TclError: dlopen(/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/tkhtml/Darwin/64-bit/Tkhtml30.dylib, 0x0006): tried: '/opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/tkhtml/Darwin/64-bit/Tkhtml30.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/Tkhtml30.dylib' (no such file), '/usr/lib/Tkhtml30.dylib' (no such file)

Environment:

miniforge3 arm64
Python 3.9.7
tkinterweb 3.10.7
MacOS 12.1 Monterey on MacBook Pro M1 Max

Other notes:

1. same code can be run without errors if I switch to anaconda packaged Python 3.8.5 (Intel) environment
2. same errors when running in either terminal command line or PyCharm, as long as with miniforge Python (arm64)

cuteufo avatar Dec 21 '21 05:12 cuteufo

Hello,

Sorry for the late reply. Thank you for letting me know about this. It looks like the backend for this project is not the right architecture for your computer. The Tkhtml30.dylib file was supposed to work across all MacOS platforms, but I guess I was wrong on that. I have uploaded Tkhtml30.zip. Try extracting the file inside the zipped folder and copying it to your Tkhtml folder (looking at your error code, it would be /opt/homebrew/Caskroom/miniforge/base/envs/tf/lib/python3.9/site-packages/tkinterweb/tkhtml/Darwin/64-bit/; just replace the existing file with the new one). I'm not sure if it would help, but it is worth a shot.

Let me know how it works out for you.

Andereoo avatar Jan 19 '22 14:01 Andereoo

I tried this out on an m1 mac, and I get mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e') with the package as installed via pip. When I use the Tkhtml30.dylib from the zip you uploaded, then I get fat file, but missing compatible architecture (have 'x86_64,i386', need 'arm64e').

pdebelak avatar Jun 13 '22 18:06 pdebelak

The backend for TkinterWeb was compiled before Apple switched from the traditional Intel chips to arm64 Apple silicon chips. Therefore, TkinterWeb should work fine on older macs, but will not load on most newer macs. I would need to re-compile Tkhtml on an m1 mac in order to resolve this issue. Unfortunately I don't have a mac to do this on, but I am trying to borrow one. In the meantime, you could try to compile Tkhtml yourself. You can download the source here. I haven't compiled anything on mac before, so I can't give you instructions, but you could definitely find information online and in COMPILE.txt and build_tkhtml.txt in the source folder. If you can compile the source code, simply replace the existing Tkhtml30.dylib file with your new file and TkinterWeb should work fine!

Andereoo avatar Jun 17 '22 12:06 Andereoo

Hi @Andereoo if you use a github Actions to do the compile you'll have access to MacOS, Linux and Windows virtual machines to compile on (or run your test cases) even different versions of those OS's see here for the platforms available https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job

I'm still learning how to do this for my project but I managed to get my testing tool to run my python app to run under all three platforms and do some basic tests, so this might be useful for you as well 🤞🏻

Dave.

damies13 avatar Nov 08 '22 22:11 damies13

Thank you very much for letting me know about this! Github Actions is a bit difficult to use because I need to compile both C and tcl files and it is not easy to browse system files with Actions, but hopefully I can get this all worked out soon!

Andereoo avatar Nov 11 '22 19:11 Andereoo

The backend for TkinterWeb was compiled before Apple switched from the traditional Intel chips to arm64 Apple silicon chips. Therefore, TkinterWeb should work fine on older macs, but will not load on most newer macs. I would need to re-compile Tkhtml on an m1 mac in order to resolve this issue. Unfortunately I don't have a mac to do this on, but I am trying to borrow one. In the meantime, you could try to compile Tkhtml yourself. You can download the source here. I haven't compiled anything on mac before, so I can't give you instructions, but you could definitely find information online and in COMPILE.txt and build_tkhtml.txt in the source folder. If you can compile the source code, simply replace the existing Tkhtml30.dylib file with your new file and TkinterWeb should work fine!

By following this thread, I have compiled the "htmlwidget" source code you provided. This is my first time compiling C, so I'm not sure if I'm in the right way. The files generated are zipped and attached here.

(My device: MacBook Air with M1 chip)

The commands I used:

$ brew install tcl-tk  # according to the thread aforementioned, the tcl-tk in Xcode won't work
$ TCL="/opt/homebrew/opt/tcl-tk"
$ cd ~/Downloads
$ tar -xzf htmlwidget.MODIFIED.zip && mv "htmlwidget (MODIFIED)" htmlwidget
$ cd htmlwidget/tkhtml
$ tclsh src/cssprop.tcl && tclsh src/tokenlist.txt
$ tclsh src/mkdefaultstyle.tcl > htmldefaultstyle.c
$ mv *.c src && mv *.h src
$ mkdir ../build && cd ../build
$ chmod 755 ../tkhtml/configure
$ ../tkhtml/configure --with-tcl="$TCL/lib" --with-tk="$TCL/lib" --with-tclinclude="$TCL/include" --with-tkinclude="$TCL/include"
$ make
$ make install

However, after I renamed "libTkhtml3.0.dylib" with "Tkhtml30.dylib" and replaced the file under "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/site-packages/tkinterweb/tkhtml/Darwin/64-bit", the sample code raised another error:

The sample code I used:

from tkinterweb import HtmlFrame
import tkinter as tk

root = tk.Tk()
frame = HtmlFrame(root)
frame.load_website("http://tkhtml.tcl.tk/tkhtml.html")  # this line failed

The error message raised:

An error has been encountered while loading http://tkhtml.tcl.tk/tkhtml.html: main thread is not in main loop.

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/site-packages/tkinterweb/htmlwidgets.py", line 174, in continue_loading
    self.load_html(data, newurl)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/site-packages/tkinterweb/htmlwidgets.py", line 416, in load_html
    self.html.reset()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/site-packages/tkinterweb/bindings.py", line 287, in reset
    self.tk.call(self._w, "reset")
RuntimeError: main thread is not in main loop

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/site-packages/tkinterweb/htmlwidgets.py", line 196, in continue_loading
    self.load_html(self.broken_page_msg)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/site-packages/tkinterweb/htmlwidgets.py", line 416, in load_html
    self.html.reset()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/tk/lib/python3.8/site-packages/tkinterweb/bindings.py", line 287, in reset
    self.tk.call(self._w, "reset")
RuntimeError: main thread is not in main loop

TsaiTung-Chen avatar Jan 10 '23 15:01 TsaiTung-Chen

Hi @TsaiTung-Chen, I don't have a Mac to test on, so I have no way to debug that error. However, there are a few things you could try. For starters, don't forget to include the following at the bottom of your code:

frame.pack(expand=True, fill="both")
root.mainloop()

If you get the same issue, try setting frame.set_maximum_thread_count(0) and let me know what happens!

Andereoo avatar Feb 02 '23 15:02 Andereoo

Hi @TsaiTung-Chen, I don't have a Mac to test on, so I have no way to debug that error. However, there are a few things you could try. For starters, don't forget to include the following at the bottom of your code:


frame.pack(expand=True, fill="both")

root.mainloop()

If you get the same issue, try setting frame.set_maximum_thread_count(0) and let me know what happens!

Thanks a lot for your reply. Setting frame.set_maximum_thread_count(0) really helps!

Now the HtmlFrame works as expected on my Apple Silicon Mac too!

Tested code:


from tkinterweb import HtmlFrame
import tkinter as tk

root = tk.Tk()
frame = HtmlFrame(root)
frame.set_maximum_thread_count(0)  # this helps
frame.load_website("http://tkhtml.tcl.tk/tkhtml.html")
frame.pack(fill='both', expand=1)
root.mainloop()

The result:

image

Would you consider adding the executable, compiled on Arm Mac, to TkinterWeb to benefit others?

TsaiTung-Chen avatar Feb 12 '23 07:02 TsaiTung-Chen

What is the current status of Mac support: still just Intel chips or arm64 as well? Seems like at least one user got the latter working. Thanks!

rodneyboyd avatar Jul 10 '23 20:07 rodneyboyd

Hi @cuteufo @pdebelak @damies13 @TsaiTung-Chen I've finally managed to compile Tkhtml using Github Actions. Apologies for the delay. Try extracting tkinterweb.zip and running __init__.py and let me know if it works.

Andereoo avatar Nov 14 '23 13:11 Andereoo

Hi @cuteufo @pdebelak @damies13 @TsaiTung-Chen I've finally managed to compile Tkhtml using Github Actions. Apologies for the delay. Try extracting tkinterweb.zip and running __init__.py and let me know if it works.

I have tried the __init__.py script with the new "libTkhtml3.0.dylib" binary but failed and got the error message:

Traceback (most recent call last):
  File "/Users/tungchentsai/Downloads/tkinterweb/__init__.py", line 30, in <module>
    frame = TkinterWeb(root)
  File "/Users/tungchentsai/Downloads/tkinterweb/__init__.py", line 19, in __init__
    master.tk.eval("package require Tkhtml")
_tkinter.TclError: dlopen(/Users/tungchentsai/Downloads/tkinterweb/libTkhtml3.0.dylib, 0x0006): tried: '/Users/tungchentsai/Downloads/tkinterweb/libTkhtml3.0.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/tungchentsai/Downloads/tkinterweb/libTkhtml3.0.dylib' (no such file), '/Users/tungchentsai/Downloads/tkinterweb/libTkhtml3.0.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))

BTW, it works fine if I replace the binary with this version.

TsaiTung-Chen avatar Nov 26 '23 03:11 TsaiTung-Chen

@TsaiTung-Chen would you be able to do me a favour and let me know if tkinterweb-simplified.zip works?

Andereoo avatar Jun 03 '24 14:06 Andereoo

@TsaiTung-Chen would you be able to do me a favour and let me know if tkinterweb-simplified.zip works?

I've tried this new version of Tkhtml dylab. It works!🎉

TsaiTung-Chen avatar Jun 04 '24 13:06 TsaiTung-Chen

@TsaiTung-Chen @damies13 @cuteufo @pdebelak @TsaiTung-Chen @G-Donkey I have finally added support for M1 Macbooks. Apologies it took me so long! Please feel free to upgrade TkinterWeb and let me know if it works for you.

Andereoo avatar Jun 04 '24 20:06 Andereoo

@Andereoo - yes, running pip install tkinterweb now works! I was able to run the example program from the readme.

pdebelak avatar Jun 05 '24 00:06 pdebelak