我可以在tkinter中制作标签可点击并获取标签的价值吗?

时间:2018-01-11 17:06:20

标签: python tkinter

我希望用户点击数字,然后数字应该改变颜色,我将能够捕获用户点击的标签,然后该表格将以文本和PDF格式保存。提前感谢mil提供任何帮助

来自tkinter import *     来自tkinter import ttk     来自tkinter import messagebox

class Proj_pres:
    """Defininf clickable labels in frame"""
    #def fr_lb(self):

    def __init__(self,master):

        master.title(" Feedback Form")
        #master.resizable(False,False)

        self.frame_header = ttk.Frame(master, borderwidth = 5, relief ='ridge').grid(sticky = NE)
        #self.frame_header.pack()

        ttk.Label(self.frame_header, text = " For recording feedback on Autumn(inerm) project presentations",
                         font=('Arial',16,'bold')).grid(row = 0, column = 0,sticky = NW)

        """Defining new frame"""
        self.frame_content = ttk.Frame(master,borderwidth = 5)
        self.frame_content.grid(row = 2, column = 0,columnspan = 3, sticky = NW)


        """Adding check buttons for Studio 1 and Studio 2"""
        self.chkb1 = IntVar()
        self.b1 = ttk.Checkbutton(self.frame_content, text = "UC1Studio1", variable = self.chkb1).grid(row =0,column = 0)

        self.chkb2 = IntVar()
        self.b2 = ttk.Checkbutton(self.frame_content, text = "UC2Studio2", variable = self.chkb2).grid(row = 0, column = 8,columnspan = 2,stick=W)

        """Adding Labels for Team and Reviewer"""

        ttk. Label(self.frame_content, text = "Team Name").grid(row =4, column = 0,sticky = W)
        ttk.Label(self.frame_content, text = "Reviewer").grid(row = 4,column = 7, sticky = E)
        ttk.Label(self.frame_content).grid(row=2, column=0)


        """Adding Entry Boxes for team name and reviewer"""
        ttk.Entry(self.frame_content).grid( row = 4, column = 1,columnspan = 4,sticky = W)
        ttk.Entry(self.frame_content).grid( row = 4, column = 8,columnspan = 2, sticky = E)

        """Adding Label and frame for grading info"""
        self.frame_info = ttk.Frame(master,borderwidth = 5, relief = 'solid')
        self.frame_info.grid(row = 3, column = 0,sticky = NW)
        ttk.Label(self.frame_info).grid(row =5,column = 0)

        ttk.Label(self.frame_info, text ="Please use the feeedback scale for each of the following criterion, "
                                            "where 5 = excellent and 1 = poor").grid(row = 7, column = 0,sticky = W)
        ttk.Label(self.frame_info).grid(row = 6, column =0)

        ttk.Label(self.frame_info,text = "OVERVIEW OF PROJECT").grid(row = 8, column = 0, sticky = NW)
        ttk.Label(self.frame_info, text = "  5: Well Structured,4: Clear aim, 3:Understandable project "
                                          "view").grid(row = 9, column = 0, sticky = NW)
        ttk.Label(self.frame_info, text="  2: Absent,1: Confused "
                                        "view").grid(row=9, column=5, sticky=NW)

        #ttk.Label(self.frame_info, text="  should come here").grid(row=9, column=1, sticky=NW)

        """Adding frame in column 2 for clickable Labels"""
        self.frame_clk=ttk.Frame(self.frame_info, borderwidth= 5, relief ='solid')
        self.frame_clk.grid(row = 9,column = 2,columnspan = 3,sticky = NW)


        self.f1_l5 = StringVar()

        l5 = ttk.Label(self.frame_clk,text = "  5  " ,
                       background = 'white',borderwidth=5,relief= 'ridge',font =('Helvetica',12,'bold'))
        #,textvariable = self.f1_l5
        l5.grid(row=0,column =1,columnspan =2 )
        f1_l4= ttk.Label(self.frame_clk, text="  4  ",background = 'white',borderwidth=5,relief= 'ridge', font=('Helvetica', 12, 'bold'))
        f1_l4.grid(row =0 , column = 3)
        f1_l3 = ttk.Label(self.frame_clk, text="  3  ",background = 'white',borderwidth=5,relief= 'ridge', font=('Helvetica', 12, 'bold'))
        f1_l3.grid(row=0, column=4)
        f1_l2 = ttk.Label(self.frame_clk, text="  2  ",background = 'white',borderwidth=5,relief= 'ridge', font=('Helvetica', 12, 'bold'))
        f1_l2.grid(row=0, column=5)
        f1_l1 = ttk.Label(self.frame_clk, text="  1  ",background = 'white', borderwidth=5,relief= 'ridge',font=('Helvetica', 12, 'bold'))
        f1_l1.grid(row=0, column=6)





        #elf.frame_content.pack()
def main():
    root = Tk()
    proj_pres = Proj_pres(root)

    root.mainloop()


if __name__ == '__main__':main()

1 个答案:

答案 0 :(得分:0)

    def clickFunction(event): #event is argument with info about event that triggered the function
        global selectedNumber #make the number visible throughout the program, don't need this if you'll just pass it as argument to function
        event.widget.config(background = "green") #event.widget is reference to widget that was clicked on and triggered the function 
        selectedNumber = 7 - event.widget.grid_info()["column"] #grid info is dictionary with info about widget's grid relative to widget, more at http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-methods.html
        if(selectedNumber > 5): selectedNumber = 5 
        print(selectedNumber)
        ''' if someday you won't use grid, but will use list to store Labels, this is a way to get Label's position
        selectedNumber = myLabels.index(event.widget)
        '''
    l5.bind("<Button-1>", clickFunction)
    f1_l4.bind("<Button-1>", clickFunction)
    f1_l3.bind("<Button-1>", clickFunction)
    f1_l2.bind("<Button-1>", clickFunction)
    f1_l1.bind("<Button-1>", clickFunction)

    ''' Alternative way for making lots of similar widgets, not to mention extending Label class
    myLabels = [] #create List to make code more compact and to be able to use loops
    for i in range(5, 0, -1):
        myLabels.append(ttk.Label(self.frame_clk, text="  " + str(i) + "  ",background = 'white',borderwidth=5,relief= 'ridge', font=('Helvetica', 12, 'bold'))
        myLabels.bind("<Button-1>", clickFunction)
        myLabels[i].grid(row =0 , column = 6 - i)
    '''

以下是您可以在下面添加的代码&#34; f1_l1.grid(row = 0,column = 6)&#34;线(约77)。但我认为您可能需要RadioButton用于此目的,因为它会自动取消标记其他选项并支持IntVar。有关活动的更多信息:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/events.html此网页包含优秀(但有点过时)的文档。

顺便说一句,在为自己制作程序时,您可能需要应用一个快速修复程序。在Python中,您可以在类及其定义之外的类中添加字段。例如。在你的代码中,你感冒后写了f1_l1.myNumber = 1&#34;创建&#34;它和clickFunction而不是grid_info()使用selectedNumber = event.widget.myNumber。它做了这件事,但是不要告诉他们我教过你;)因为它不被认为是以这种方式添加字段的良好做法。

如果您有任何其他问题,请随时提出。