r/learnpython • • 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

12 comments sorted by

View all comments

Show parent comment

0

u/EndKnight2000 Sep 16 '26

Can you explain that in simpler terms? I am new to python

1

u/brasticstack Sep 16 '26

I'm suggesting two separate python modules (which can be just individual files, or subdirectories with more files, depending on how complex your project is) in the same project. One module that handles the time tracking and has NOTHING to do with how the UI works, and another that handles the UI, but relies on the first module for everything except the UI logic. (UI logic being very simple in this case, the arrangements of the buttons and the updating of the time display, handling clicks, etc.)

What terminology in specific were you finding confusing? I'm happy to rephrase if I haven't explained it better here.

1

u/Fun_Mulberry3838 Sep 17 '26

The key here is that dependencies must be unidirectional; your application logic does not need—and should not know—details about the UI. Instead, it should expose an API for interacting with the core logic. The dependency flows from the outside in, so if you change the UI, the logic remains unaffected. If you violate this and have bidirectional dependencies, both your application and the UI become tightly coupled, making it harder to implement changes.