1
\$\begingroup\$

I'm starting to build an application for automated network troubleshooting. It's going without any issues so far. I'm using an OOP approach @ which i create the widgets in the init method. what's bothering me is that when the application starts getting more complex (Ex: Adding way more widgets); the init method will get more crowded limiting the visibility of the code.

Is there a better way to create the widgets without crowding the init() method?

from tkinter import *
import customtkinter

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.title("Automated TShooting Application")
        self.geometry("587x450+500+50")
        self.iconbitmap("Sharingan.ico")
        self.resizable(False, False)

        self.mainframe = customtkinter.CTkFrame(self, width=400, height=400)
        self.mainframe.grid(row=0, column=0, padx=20, pady=(20, 20))

        self.username_label = customtkinter.CTkLabel(self.mainframe, text="Username")
        self.username_label.grid(row=0, column=0, padx=20, pady=(20, 0))
        self.username_entry = customtkinter.CTkEntry(self.mainframe)
        self.username_entry.grid(row=1, column=0, padx=20)

        self.password_label = customtkinter.CTkLabel(self.mainframe, text="Password")
        self.password_label.grid(row=2, column=0)
        self.password_entry = customtkinter.CTkEntry(self.mainframe)
        self.password_entry.grid(row=3, column=0, padx=20, pady=(0, 20))

    

if __name__ == "__main__":
    customtkinter.set_appearance_mode("light")

    app = App()
    app.mainloop()
\$\endgroup\$
3
  • \$\begingroup\$ This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does \$\endgroup\$
    – Mast
    Commented Nov 12, 2022 at 11:38
  • \$\begingroup\$ Seems to me that you have not read the question at all. There is nothing wrong with the code, im asking about the right approach for when things get more complex. \$\endgroup\$
    – homunculus
    Commented Nov 12, 2022 at 17:05
  • \$\begingroup\$ This is Code Review. We can review your code. If you don't want to tell us what your code is doing and why, to help you improve your code, I think you got the wrong site. \$\endgroup\$
    – Mast
    Commented Nov 12, 2022 at 17:35

1 Answer 1

1
\$\begingroup\$

Using classes in the code doesn't automatically make it OOP. In this case you're not using any of its advantages (encapsulation, inheritance, polymorphism, composition, etc.) so there is no point in having all those labels and entries as fields of the App class.

Generally, if you're planning on expanding the app, it would be a good idea to extract LoginForm and PasswordForm into their own classes. However I'm not sure if the library you're using allows that. Even the "complex example" written by the author(!) of the lib uses the wall-of-text approach, so it doesn't look like OOP approach is even supported.

\$\endgroup\$
2
  • \$\begingroup\$ As i mentioned im just starting the application so the code is still rather simple & does not require the more advanced options of OOP. If you are suggesting that i should create a class for each widget; then this will just make the code even more messy. \$\endgroup\$
    – homunculus
    Commented Nov 12, 2022 at 17:08
  • \$\begingroup\$ @homunculus I'm suggesting that "if you're planning on expanding the app". Otherwise you don't need any classes at all. \$\endgroup\$ Commented Nov 12, 2022 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.