{"id":18388895,"url":"https://github.com/imperialcollegelondon/pendulum-tkinter-testcase","last_synced_at":"2025-10-19T13:14:23.750Z","repository":{"id":220126492,"uuid":"640996582","full_name":"ImperialCollegeLondon/pendulum-tkinter-testcase","owner":"ImperialCollegeLondon","description":"Simple test case to show how to use Python and Tkinter to manage a pendulum simulation","archived":false,"fork":false,"pushed_at":"2024-01-31T11:53:15.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-16T00:45:47.112Z","etag":null,"topics":["conda-environment","pendulum","python3","simulation","tkinter"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ImperialCollegeLondon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-05-15T14:57:37.000Z","updated_at":"2023-05-15T15:29:19.000Z","dependencies_parsed_at":"2024-01-31T12:58:42.945Z","dependency_job_id":null,"html_url":"https://github.com/ImperialCollegeLondon/pendulum-tkinter-testcase","commit_stats":null,"previous_names":["imperialcollegelondon/pendulum-tkinter-testcase"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImperialCollegeLondon%2Fpendulum-tkinter-testcase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImperialCollegeLondon%2Fpendulum-tkinter-testcase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImperialCollegeLondon%2Fpendulum-tkinter-testcase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImperialCollegeLondon%2Fpendulum-tkinter-testcase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ImperialCollegeLondon","download_url":"https://codeload.github.com/ImperialCollegeLondon/pendulum-tkinter-testcase/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248515673,"owners_count":21117221,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conda-environment","pendulum","python3","simulation","tkinter"],"created_at":"2024-11-06T01:38:49.062Z","updated_at":"2025-10-19T13:14:23.615Z","avatar_url":"https://github.com/ImperialCollegeLondon.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pendulum Tkinter testcase\n\nThis is a simple test case to show how to build a Graphical User Interface (GUI) using [Tkinter](https://docs.python.org/3/library/tkinter.html) in Python.\n\nThe rationale for choosing Tkinter is that it's included in any modern distribution of Python, it's easy to use, and it works cross-platform. There are other more expressive frameworks available (e.g., [guikit](https://github.com/ImperialCollegeLondon/guikit/tree/develop)), but this is a simple solution for putting together simple and portable interfaces for teaching and research.\n\n## Code\n\nThe single *pendulum_tkinter_testcase.py* Python file contains two classes and the main function.\nThe classes are:\n\n* **Pendulum**: simple simulation of the movement of a pendulum given its mass, wire length, and starting angle. The simulation runs in a separated thread. The Pendulum class is not aware of the GUI.\n* **Window**: Tkinter management, from layout to user events, from canvas animation to Matplotlib plotting.\n\nThere are class constants that can be tweaked as needed, especially about the simulation and animation update rates.\n\n### Widgets\n\nThe code shows how to create an interactive GUI with [tkinter.ttk](https://docs.python.org/3/library/tkinter.ttk.html) [buttons](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Button.html), [checkboxes](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Checkbutton.html), [sliders](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Scale.html), and [text fields](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Entry.html) arranged using tk [grid](https://tkdocs.com/tutorial/grid.html) and pack layouts.\n\nIn addition, it shows how to add a tk [canvas](https://tkdocs.com/tutorial/canvas.html), use it to draw simple shapes, and animate them.\n\nFinally it [embeds](https://matplotlib.org/3.1.0/gallery/user_interfaces/embedding_in_tk_sgskip.html) a [Matplotlib](https://matplotlib.org/) plot that is updated while the animation runs. \n\n## Environment\n\nThe code has been developed and tested on Ubuntu 22.04 LTS with [conda](https://docs.conda.io/en/latest/). In the *conda* directory of this repository there is a file that can be used to create the same setup on different systems. \n\nYou can create a working *pendulum_tkinter_testcase* conda environment by running:\n```\nconda env create -f environment.yml\n```\n\nIf you prefer to create the environment by yourself, you can run\n```\nconda create --name pendulum_tkinter_testcase -c conda-forge python=3.11.3 tk matplotlib imageio imageio-ffmpeg\n``` \nthat will add all the required dependencies.\n\nPlease note that the two commands work with [mamba](https://github.com/mamba-org/mamba) as well.\n\nIf you do not want to use conda, you can just install the same required dependencies to your Python distribution. \n\n## Look\n\nThe code uses [tkinter](https://docs.python.org/3/library/tkinter.html) and [tkinter.ttk](https://docs.python.org/3/library/tkinter.ttk.html) modules. The second provides access to the Tk themed widget set, so it gives options to customise the look of your GUI. Ttk widgets often have less options than the equivalent Tkinter ones, but in this case they are good enough.\n\nIf you prefer a more modern style you can try [ttkbootstrap](https://github.com/israel-dryer/ttkbootstrap) instead. It was not used for this test case because it isn't available in conda and should be installed using [pip](https://packaging.python.org/en/latest/key_projects/#pip). \n\nWhen it's available you can use it instead of tkinter.ttk by changing the line\n```\nfrom tkinter import ttk\n```\nin *pendulum_tkinter_testcase.py* with\n```\nimport ttkbootstrap as ttk\n```\n\n## Packaging\n\nIf can use [PyInstaller](https://pyinstaller.org) to bundle the program and all its dependencies into a single package. \nYou can install PyInstaller directly in conda by running\n```\nconda install pyinstaller\n```\n\nTo use it, type\n```\npyinstaller --onefile --hidden-import=PIL --hidden-import=PIL._imagingtk --hidden-import=PIL._tkinter_finder pendulum_tkinter_testcase.py\n```\n\nPyInstaller will create the *dist* directory containing the single file to distribute. The additional parameters are required to include all the dependencies needed to support ImageTk (see this [post](https://stackoverflow.com/questions/52675162/pyinstaller-doesnt-play-well-with-imagetk-and-tkinter)). \n\nPlease note that the resulting executable file is platform dependent, so you need to repeat the process in each operating system you plan to support.\nOn Windows, the resulting executable may be detected as malware by some antivirus. See this [post](https://stackoverflow.com/questions/77257748/pyinstaller-exe-marked-as-virus) for additional details. \n\n## Limitations\n\nThe simulation and the GUI are quite minimal. The main thing missing is the possibility to interactively see the changes in the pendulum bob weight and wire length before starting the simulation. The main idea was to show how to create a useful test case in less than 300 lines of code (comments included). This is the reason why thre are also long lines that could be splitted. \n\nImproving the code is a nice way to experiment with Tkinter and understand how it works, so it's left to the user.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimperialcollegelondon%2Fpendulum-tkinter-testcase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimperialcollegelondon%2Fpendulum-tkinter-testcase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimperialcollegelondon%2Fpendulum-tkinter-testcase/lists"}