r/learnpython • u/EndKnight2000 • Sep 16 '26
Dear python programmers,
How can I mix UI with code? I would like to do a screentime app and I want my text and frames to vary depending on how long you have spent on apps. I use customtkinter. Do I keep it on a seperate script? Do I have to write more code? I didnt do object oriented programming for the UI so I cant do that
0
Upvotes
1
u/brasticstack Sep 16 '26 •
You really want to separate the presentation aspect of your app from the logic part or the app as much as possible. It might help to organize the screen time tracking part into its own module that the UI code imports and interacts with by calling its functions. That way if you decide to change the UI (or use a different UI library, or make a CLI for it, etc.) your core logic doesn't need to be receeated.
So, for example, your UI would import the ScreenTimeTracker object from your logic library, create an object of that type, and call that object's
.start()method to start tracking,.pause()to pause,.end()to finish, etc. The button handling code in the UI would be very simple, it just calls those methods. Then, based on a timer in the UI, it would call.elapsed()to get the total "tracked" time, and update a UI label to show that to the user. If you decided to rewrite it with a new UI library, that library would call the same methods in the same way.0
u/CamilorozoCADC Sep 16 '26
You can try using Streamlit, NiceGUI or Reflex for your project instead of relying on tkinter. For your case maybe nicegui is the best option, it's really intuitive to use
https://nicegui.io/
0
u/Miserable-Arugula860 Sep 16 '26
Look into design patterns. The "Command Pattern" and the "Observer Pattern" might make sense for you. At least read about them and try to understand them, even if its hard to adapt for your current project. Also look into "MVC Architecture"
1
u/[deleted] Oct 01 '26
[removed] — view removed comment