简体   繁体   English

如何使用类使用tkinter创建多个相似框架?

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

How would I create multiple frames that have the same widgets in Tkinter? 如何在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. 基本上,我想做的是为一组多个帧创建15个副本,每个副本都包含图像中所示的相同小部件,该程序的目的是帮助用户根据由提供的特定ID将照片分为几组用户。 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. 复制代码15次不是很有效,我想知道是否可以使用一个类一次定义框架并为每个新框架重用代码。 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. 用户对所有照片进行分类后,单击按钮,然后应使用新ID保存所有照片,并将单选按钮的信息保存到csv文件中。 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. 我不想重复15次。

  ############################################################################
#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: 现在,此类PhotoFrame可以根据需要多次使用,尽管请理解您必须适当地配置每个框架,例如,我将忽略label_widget部分中的text属性分配(您不希望所有您的框架标题为“框架3”),因此在主程序逻辑中,您将像这样配置PhotoFrame对象的每个实例:

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 我希望这对您有帮助-如果您不熟悉类,请查阅提供的文档,这是一个很好的tkinter参考: NMT tkinter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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