简体   繁体   中英

How do I create multiple similar frames with tkinter using classes?

How would I create multiple frames that have the same widgets in Tkinter? Basically what I want to do is create 15 copies of a set of multiple frames that all contain the same widgets as shown in the image, the purpose of this program is to assist the user in sorting photographs into groups based on a specific ID supplied by the user. The radio buttons are there for the user to classify each photo, ie front, back ,top etc..

Its not very efficient to copy the code 15 times and I want to know if it's possible to use a class to define the frame once and reuse the code for each new frame. I need to keep track of what the user does on each frame and save their selections on the radio buttons and check boxes for each frame. After all the photos have been classified by the user, a button is clicked that should then save all the photos with a new ID and also saves the info from the radio buttons into a csv file. Then the next batch of photos is loaded and the process repeats.

I have included an example of the code I used to create one of the frames, this is the code that I want to make reusable. I do not want to have to repeat it 15 times.

  ############################################################################
#FRAME 3
Photo_2 = Frame(master, bg = "white",relief = RIDGE, bd = 2)
Photo_2.grid(column = 2, row = 1, padx=5, pady=5)
Lbl2 = Label(Photo_2,text = 'Frame 3')
Lbl2.grid(row = 0, column = 0, columnspan = 4, pady = 5)

# Check box
varc2 = StringVar()
varc2.set(0)
Check_2 = Checkbutton(Photo_2, variable = varc2, text="Relevant?", command = lambda:Chk_Val(varc2))
Check_2.grid(row = 1,column = 0,columnspan = 4)

# Photo 1
P2 = "Photo_2.jpg"
P2 = Image.open(P2).resize((200, 200), Image.ANTIALIAS)
phot2 = ImageTk.PhotoImage(P2)
panel = Label(Photo_2, image = phot2)
panel.grid(columnspan = 3, column=1)

# Create Multiple Radio Buttons
Rad_Cont = Frame(Photo_2)
Rad_Cont.grid(column = 0, row = 2)

v2 = StringVar()
v2.set("Address")

for text,mode in RADIO:
    b = Radiobutton(Rad_Cont, text=text, variable=v2,
                    value=mode, command = lambda:Rad_Val(v2))
    b.pack()

################################################################################

Of course it is possible to create a class to represent similar objects.

Here is how I might implement what you're trying to accomplish:

import tkinter as tk

class PhotoFrame(tk.Frame):

    def __init__(self, master):

        tk.Frame.__init__(self, master, bg='white', relief='ridge', bd=2)

        self.label_widget()
        self.checkbox_widget()
        self.photo_widget()
        self.radio_widgets()


    def label_widget(self):
        self.title_label = tk.Frame(self, text='Frame 3')  # Or Frame 1, 2 etc.
        self.title_label.grid(row=0, column=0, columnspan=4, pady=5)


    def checkbox_widget(self):
        self.varc = tk.StringVar()
        self.varc.set(0)

        self.checkbox = tk.Checkbutton(self, variable=self.varc,
                                       text='Relevant?', command=self.Chk_Val)
        self.checkbox.grid(row=1, column=0, columnspan=4)


    def photo_widget(self):
        # ... Your code here


    def radio_widgets(self):
        # ... Your code here


    def Chk_Val(self):
        # ... Your code here

Now I tried not to give you the entire solution so you can learn and figure the rest out by yourself, but I'm sure you can see what I'm getting at in terms of using a class. Now this class PhotoFrame could be used as many times as you'd like, although please understand you'll have to configure each frame appropriately, eg I would omit the the text attribute assignment in the label_widget section (You don't want all of your frames titled 'Frame 3'), so in your main program logic, you'd configure each instance of a PhotoFrame object like so:

frame1 = PhotoFrame(master)
frame1.title_label.configure(text='Frame 1')  # Along with any other configuration

I hope this all helps - if you are not familiar with classes check out the provided documentation, and here is a great tkinter reference: NMT tkinter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM