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

Briefcase run does not include Tkinter #318

Closed
saroad2 opened this issue Mar 10, 2020 · 19 comments
Closed

Briefcase run does not include Tkinter #318

saroad2 opened this issue Mar 10, 2020 · 19 comments

Comments

@saroad2
Copy link
Member

saroad2 commented Mar 10, 2020

This is going to be quite long, hope It's Ok

I have a Briefcase project that shows matplotlib plots from a compact GUI.
I'm working on Windows with python3.7. When I run my program using briefcase dev it's working ok, but when trying to run it with breifcase run it fails.
I went down the rabbit hole and saw that on windows it runs the program using windows\src\FORMAL_NAME\python\pythonw.exe -m MY_PACKAGE_NAME. I tried to run it manually and saw that nothing is printed, not an error and not anything else.

I saw that in the same directory there is a python.exe file so I ran it with -m MY_PACKAGE_NAME and I got the following error:

Traceback (most recent call last):
  File "D:\obj\windows-release\37win32_Release\msi_python\zip_win32\runpy.py", line 193, in _run_module_as_main
  File "D:\obj\windows-release\37win32_Release\msi_python\zip_win32\runpy.py", line 85, in _run_code
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app\eddington_gui\__main__.py", line 1, in <module>
    from eddington_gui.app import main
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app\eddington_gui\app.py", line 6, in <module>
    from eddington import read_data_from_excel, InvalidDataFile, plot_fitting, plot_residuals, PlotConfiguration
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app_packages\eddington\__init__.py", line 18, in <module>
    from eddington.plot.residuals import plot_residuals
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app_packages\eddington\plot\residuals.py", line 1, in <module>
    from eddington.plot.util import title, label_axes, horizontal_line, plot_data, show_or_export
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app_packages\eddington\plot\util.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app_packages\matplotlib\pyplot.py", line 2282, in <module>
    switch_backend(rcParams["backend"])
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app_packages\matplotlib\pyplot.py", line 221, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "D:\obj\windows-release\37win32_Release\msi_python\zip_win32\__init__.py", line 127, in import_module
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app_packages\matplotlib\backends\backend_tkagg.py", line 1, in <module>
    from . import _backend_tk
  File "C:\Users\saroa\repositories\eddington_gui\windows\EddingtonGUI\src\\app_packages\matplotlib\backends\_backend_tk.py", line 6, in <module>
    import tkinter as tk
ModuleNotFoundError: No module named 'tkinter'

I looked at the windows\src\FORMAL_NAME\python\ and I saw that indeed Tkinter is not there.
Is that on purpose? how can I manually add Tkinter to there?

@freakboy3742
Copy link
Member

Thanks for the report. So the issue here isn't briefcase specifically - it's the support package.

The support packages that Briefcase uses by default don't include tkinter (on purpose) - this is because getting a working tkinter build can be complicated. tkinter support requires external libraries that aren't always available, and they can't be obtained easily using purely Python tooling.

In the case of the Windows support package specifically, briefcase is actually using the Python-issued "embedded" distribution, which also doesn't include tkinter (on purpose, for the same reason).

It is entirely possible to support tkinter - however, you need to provide your own support package that includes tkinter and all it's additional dependencies.

If your requirement is around matplotlib specifically, you may have more luck using a different plotting backend. matplotlib has a pluggable rendering backend, and there are backends for widget toolkits other than tkinter (Qt, for example). Using one of those backends may be an easier option than trying to get tkinter support, as those widget kits have better Python installation support.

@freakboy3742
Copy link
Member

One more note - at the very least, we should document that Tkinter isn't supported. Ideally, we'd be able to identify that the project won't work because of the support package - but programatically identifying that will be complex.

@saroad2
Copy link
Member Author

saroad2 commented Mar 11, 2020

Hey, replaced usage of Tkinter with WX and it works!

Thank you very much :)
Closing the issue.

@mocaccano
Copy link

Hi @freakboy3742, I'm facing the same issue at the moment and found this issue.
How can I explicitly add tkinter as a support package? Unable to figure this out, as by default, it just comes with python.
Like @saroad2 , briefcase dev works fine, but not briefcase run. Appreciate any help, thanks!

@freakboy3742
Copy link
Member

@mocaccano It's not trivial, and I can't point you at an easy set of instructions to include it. While Tkinter is part of the standard library, it isn't part of the Windows embedded install - because it's not easy to redistribute. That's the same reason that we don't include it in the macOS support package that Briefcase uses.

@mocaccano
Copy link

Thanks for the reply. If I replace the part of the code which uses Tkinter by PySide6 which is one of the GUI options proposed by Briefcase, would it solve the problem of packaging?

@freakboy3742
Copy link
Member

@mocaccano Yes, you can use Briefcase to package PySide packages. You can also use Toga, BeeWare's own GUI framework.

@jhi2
Copy link

jhi2 commented Mar 22, 2024

Hey I think I have a copy of tkinter for python 3.12 from my install of it.
You can download it at my site at: https://internalfiles.johnnytech.net/githubfiles/briefcase-tkinter-issue/tkinter.zip.

@jhi2
Copy link

jhi2 commented Mar 22, 2024

@freakboy3742
Copy link
Member

@jhi2 Thanks for sharing that.

On this note; we're currently landing our iOS patches in CPython, which has required us to rework how CPython is built for iOS. This has had the unexpected side effect that with very few modifications, we're able to use the officially distributed CPython binary for macOS apps as the support package for macOS - which includes support for Tkinter.

It will likely still be a couple of months before this becomes "production ready", but you can see the work in progress here.

@jhi2
Copy link

jhi2 commented Mar 22, 2024

@jhi2 Thanks for sharing that.

On this note; we're currently landing our iOS patches in CPython, which has required us to rework how CPython is built for iOS. This has had the unexpected side effect that with very few modifications, we're able to use the officially distributed CPython binary for macOS apps as the support package for macOS - which includes support for Tkinter.

It will likely still be a couple of months before this becomes "production ready", but you can see the work in progress here.

Well, I have Windows

@cylewis
Copy link

cylewis commented May 30, 2024

@jhi2 I'm in the process of making a GUI app which uses tkinter on python 3.10 that I want to work on windows and macOS. How do I use your support package to allow briefcase to work?

@freakboy3742
Copy link
Member

@cylewis The short version is that you need to compile a version of the CPython support packages that include tkinter. This is not a simple task, as Tkinter isn't a simple thing to distribute - the official Windows embedded package (which is what Briefcase uses) doesn't include tkinter specifically because of the difficulties of packaging tkinter.

Honestly - my suggestion is that you need to have a very good reason to use tkinter in this day and age. There are much better GUI toolkits available these days, and they're not subject to the distribution issues that tkinter has.

@cylewis
Copy link

cylewis commented May 31, 2024

@freakboy3742 Thanks for the response, I chose customtkinter as the GUI library to build my app with 6 months ago, if i knew then what I know now, i would not have used it. I guess my very good reason to use tkinter is that I already have the app made using it. I'll look into your suggestion though, it might be a bit outside my skill level.

@jhi2
Copy link

jhi2 commented May 31, 2024 via email

@jhi2
Copy link

jhi2 commented Jun 1, 2024 via email

@drew0ps
Copy link

drew0ps commented Jul 27, 2024

I was wondering about the same thing - it is just way easier to create nice looking gui applications with customtkinter - everything seems to be way more intuitive. An integration would be very welcome, though I understand that toga was made to replace non-pythonic gui apps.

@rmartin16
Copy link
Member

rmartin16 commented Jul 27, 2024

FWIW, I was able to use tkinter with Briefcase.

Warning: This is intended purely as a proof-of-concept; use at your own risk.

image

  1. Replace Briefcase's default Python support package with one provided by Standalone Python, for instance:
support_package = "https://github.com/indygreg/python-build-standalone/releases/download/20240726/cpython-3.11.9+20240726-x86_64-pc-windows-msvc-install_only_stripped.tar.gz"
  1. Run briefcase create windows visualstudio
  2. Update Main.cpp for the stub
    • Update PythonHome: python_home = Application::StartupPath + "\\python";
    • Add Lib, DLLs, and libs to module_search_paths instead of the python3.x.zip
    • For instance:
    path = python_home + "\\Lib";
    debug_log("- %S\n", wstr(path));
    status = PyWideStringList_Append(&config.module_search_paths, wstr(path));
  1. Run briefcase run windows visualstudio

@ItsCubeTime
Copy link

The first answer on https://stackoverflow.com/questions/37710205/python-embeddable-zip-install-tkinter does the trick. Though, it forgets to mention that you also need the tkinter folder under site-packages (which goes anywhere you like as long as it gets added to your sys.path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants