在python上添加标签

时间:2016-05-08 08:56:33

标签: python tkinter frame

我想在标签上创建框架并尝试了很多代码,但它没有成功。还试图在没有框架的情况下在屏幕左侧制作检查按钮。谁能帮我?谢谢

我走得很远 1st Try

但我希望用框架

使它看起来像这样

enter image description here

show_status = Label(dashboard, bd = 5, text = 'Even', fg = 'black',
                        font = ('Arial', 70), width = 8)

def update_dashboard():

three_buttons = Label(dashboard, relief = 'groove')

Alpha_button = Checkbutton(three_buttons, text = 'Alpha',
                              variable = alpa_1,
                              command = update_dashboard)

Beta_button = Checkbutton(three_buttons, text = 'Beta',
                           variable = beta_2, 
                           command = update_dashboard)

Gamma_button = Checkbutton(three_buttons, text = 'Gamma',
                           variable = gemma_3, 
                           command = update_dashboard)


Alpha_button.grid(row = 1, column = 0, sticky = 'w')
Beta_button.grid(row = 1, column = 2, sticky = 'w')
Gamma_button.grid(row = 1, column = 4, sticky = 'w')

margin = 5 # pixels

show_status.grid(padx = margin, pady = margin, row = 1,
                     column = 1, columnspan = 2,)
three_buttons.grid(row = 4, column = 2, sticky = W)


dashboard.mainloop()

1 个答案:

答案 0 :(得分:1)

您可以使用Frame或Canvas并在其上绘制其余小部件。让我们依靠grid layout manager来使用框架。

要获得您正在寻找的效果,您只需使用columnspan选项选项将标签跨越检查按钮小部件的3列。

完整程序

这是一个使用面向对象概念的简单解决方案:

'''
Created on May 8, 2016

@author: Billal Begueradj
'''
import Tkinter as Tk


class Begueradj(Tk.Frame):
    '''
    Dislay a Label spanning over 3 columns of checkbuttons
    '''
    def __init__(self, parent):
        '''Inititialize the GUI
        '''
        Tk.Frame.__init__(self, parent)
        self.parent=parent
        self.initialize_user_interface()

    def initialize_user_interface(self):
        """Draw the GUI
        """
        self.parent.title("Billal BEGUERADJ")       
        self.parent.grid_rowconfigure(0,weight=1)
        self.parent.grid_columnconfigure(0,weight=1)
        self.parent.config(background="lavender")    

        # Create a Frame on which other elements will be attached to
        self.frame = Tk.Frame(self.parent, width = 500, height = 207)  
        self.frame.pack(fill=Tk.X, padx=5, pady=5)   

        # Create the checkbuttons and position them on the second row of the grid
        self.alpha_button = Tk.Checkbutton(self.frame, text = 'Alpha', font = ('Arial', 20))
        self.alpha_button.grid(row = 1, column = 0)

        self.beta_button = Tk.Checkbutton(self.frame, text = 'Beta', font = ('Arial', 20))
        self.beta_button.grid(row = 1, column = 1)

        self.gamma_button = Tk.Checkbutton(self.frame, text = 'Gamma', font = ('Arial', 20))
        self.gamma_button.grid(row = 1, column = 2)

         # Create the Label widget on the first row of the grid and span it over the 3 checbbuttons above
        self.label = Tk.Label(self.frame, text = 'Even', bd = 5, fg = 'black', font = ('Arial', 70), width = 8, relief = 'groove')
        self.label.grid(row = 0, columnspan = 3)



# Main method
def main():
    root=Tk.Tk()
    d=Begueradj(root)
    root.mainloop()

# Main program       
if __name__=="__main__":
    main()

演示

以下是正在运行的程序的屏幕截图:

enter image description here