性别选项中的Tkinter网格

时间:2017-11-26 09:23:15

标签: python-2.7 tkinter

我们如何设置性别网格,使它们只接近2列 我想要性别(标签),男性和女性(选项)包装成2列 这是我的代码......

from Tkinter import *
root = Tk()
root.geometry('500x500')
root.title('Registration Form')

head = Frame(root)
entry = Frame(root)

head.pack()
entry.pack()

var = StringVar()

heading = Label(head,text='Registration Form',font='Helvetica 25 bold',pady=10).pack()

name_label = Label(entry,text='Name  ',font='Helvetica 10 bold',pady=5)
name_entry = Entry(entry,width=30, font='Helvetica 10')

email_label = Label(entry,text='Email  ',font='Helvetica 10 bold',pady=5)
email_entry = Entry(entry,width=30, font='Helvetica 10')

mob_label = Label(entry,text='Mobile  ',font='Helvetica 10 bold',pady=5)
mob_entry = Entry(entry,width=30, font='Helvetica 10')

gender_label = Label(entry,text='Gender  ',font='Helvetica 10 bold',pady=5)
male = Radiobutton(entry,text='Male',variable=var,value='male')
female = Radiobutton(entry,text='Female',variable=var,value='female')

name_label.grid(row=1, column=1)
name_entry.grid(row=1, column=2)
email_label.grid(row=2, column=1)
email_entry.grid(row=2, column=2)
mob_label.grid(row=3, column=1)
mob_entry.grid(row=3, column=2)
gender_label.grid(row=4, column=1)
male.grid(row=4,column=2, sticky="nsew")
female.grid(row=4,column=2,sticky="nsew")

root.mainloop()

1 个答案:

答案 0 :(得分:1)

malefemale放入下一栏,并使用columnspan=2作为Entry

from tkinter import *

root = Tk()

name_label = Label(root, text='Name')
name_entry = Entry(root)

gender_label = Label(root, text='Gender')
male = Radiobutton(root, text='Male')
female = Radiobutton(root, text='Female')

name_label.grid(row=1, column=1)
name_entry.grid(row=1, column=2, columnspan=2)

gender_label.grid(row=4, column=1)
male.grid(row=4, column=2, sticky="nsew")
female.grid(row=4, column=3, sticky="nsew")

root.mainloop()

enter image description here

参见effbot.org:The Tkinter Grid Geometry Manager