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
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
-2
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
Oct 01 '26
[removed] — view removed comment
1
u/EndKnight2000 Oct 11 '26
I want to make a bar that gets taller so what I should do is use the widget.configure(text = total_screentime) ?
1
u/EndKnight2000 Oct 12 '26
When I do it that doesn't work it gives me an error saying that my variables from the other script are not defined?
1
u/EndKnight2000 Oct 12 '26
I just saw a tutorial and after a bit of changing my code I got the script global by importing it and writing the file name before the variable thank you!
1
u/EndKnight2000 Oct 12 '26
Oh wait I found a bug when I import the script there is no more UI shown?
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.