-1

I'm trying to familiarize myself with tkinter and I created a dummy "app" which has multiple frames and in each frames has multiple widgets.

For example:

import tkinter as tk

if __name__ == "__main__":

    window = Tk()
    window.title("somename")
    window.geometry("400x400")

    frame1 = tk.Frame(window) 
    frame1.grid(row = 0, column = 0) 
    label1 = tk.Label(frame1, text = "this is frame1") 
    label1.pack() 
    button1 = tk.Button(frame1) 
    button1.pack()

    frame2 = tk.Frame(window) 
    frame2.grid(row = 1, column = 0) 
    label2 = tk.Label(frame2, text = "this is frame2") 
    label2.pack() 
    button2 = tk.Button(frame2) 
    button2.pack()

So far this format works, however its very messy and I also need the return values of the buttons using command (like filename from askopenfilename, values from CheckButton, etc) and I think OOP will be a good choice as it encapsulates information, however, am not very familiar with OOP as I mostly use python for scripting in jupyter.

My question is, if I should use classes, how should I partition the class? Should I separate each frames with elements in it as an object, or something else?

Edit: I should've said that each frame has a label but different type of widgets. Thank you @Henry Yik for the link, but my question is whether using class necessary when the only similarity between frames are the frame itself and a label, and nothing else.

wowzaaa
  • 141
  • 2
  • 12
  • I think [this post](https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application) is what you are looking for. – Henry Yik Jun 23 '20 at 09:10

1 Answers1

0

You should probably use a class for each group. The most common way to do this is to have the class inherit from Frame. That lets you treat the whole group of widgets as a single compound widget when trying to lay everything out.

For example, one of the frames might look like this:

class Frame1(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        label1 = tk.Label(self, text = "this is frame1") 
        button1 = tk.Button(self) 

        label1.pack() 
        button1.pack()

Notice that all of the child widgets are in self rather than in the root window.

You can then treat this class as if it were a regular widget. For example:

root = tk.Tk()
frame1 = Frame1(root)
frame1.pack()

If each of your frames is identical, you can create a base class from which all other frames inherit. If they are all different, you don't need the base class. Even if the frames share almost nothing in common, using classes is a good way to logically treat a group of widgets as a single object.


After I wrote this answer you modified your question to say "my question is whether using class necessary when the only similarity between frames are the frame itself and a label, and nothing else."

The similarity isn't the issue. Classes are useful whether they share a common base or not. Classes make it possible to encapsulate a group of related functionality into a single entity.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Each frame has a label, but different types of widgets, for example, frame1 has "select file button", frame2 has checkboxes, frame3 has drop-down menu. I think using class will over-complicate things as the only thing present in each frame is the frame itself. However, not using class will make it look less professional so to speak – wowzaaa Jun 23 '20 at 13:41
  • @wowzaaa: What do you mean by "the only thing present in each frame is the frame itself"? You said each frame will have different types of buttons. Those two statements are contradictory. You also wrote that you think OOP is the right choice, so I gave an OOP-based answer. – Bryan Oakley Jun 23 '20 at 13:44
  • sorry for the confusion, it appears that I was confused with my own question too. I meant that each frames has a widget and a label and need return value of the widget, but the widget differs between frames. I think using OOP might be appropriate because many of the tkinter implementation structured in class, now I think it can partially be structured around instantiating Class for frame, label and get return value of widget. What do you think about this? Sorry if this is confusing, am a new user of tkinter. – wowzaaa Jun 23 '20 at 13:50