Python,我如何获取要附加到标签的条目

时间:2018-09-22 18:11:44

标签: python python-3.x tkinter

from tkinter import*

class contactMain:

    def __init__(self, master):
        #First Label Heading
        self.heading = Label(master, text='CONTACT REGISTRY', bg='blue', fg='white', width=30)
        self.heading.grid(row=0,column=0)
        #Space between the heading and the first
        self.spaceHF = Label(master,bg='blue',fg='white', width=30)
        self.spaceHF.grid(row=1, column=0)
        #firstName label and entry
        self.fNameLabel = Label(master,text='First Name' ,bg='white', fg='blue',width=15)
        self.fNameLabel.grid(row=2,column=0,sticky=W)

        self.fNameEntry = Entry(master)
        self.fNameEntry.grid(row=2,column=1)

root = Tk()
app = contactMain(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

您的问题不是很清楚,但我认为您希望标签旁边的条目。这可以通过使标题跨几列来完成:columnspan=2。我还拉伸标题以填充单元格的宽度:sticky=EW。看一下这个例子。那是你想要的吗?

from tkinter import*

class contactMain:
    def __init__(self, master):
        #First Label Heading
        self.heading = Label(master, text='CONTACT REGISTRY', bg='blue',fg='white', width=30)
        self.heading.grid(row=0,column=0, columnspan=2, sticky=EW)
        #Space between the heading and the first
        self.spaceHF = Label(master,bg='blue',fg='white', width=30)
        self.spaceHF.grid(row=1, column=0, columnspan=2, sticky=EW)
        #firstName label and entry
        self.fNameLabel = Label(master,text='First Name' ,bg='white', fg='blue',width=15)
        self.fNameLabel.grid(row=2,column=0,sticky=W)

        self.fNameEntry = Entry(master)
        self.fNameEntry.grid(row=2,column=1)

root = Tk()
app = contactMain(root)

root.mainloop()