如何在tkinter中对齐标签,条目

时间:2018-02-27 11:11:18

标签: python tkinter tkinter-entry tkinter-layout

我真的很沮丧,试图在tkinter中对齐标签和输入按钮。 我想创建一个如下所示的GUI

http://take.ms/0Mr8b

上面的页面使用页面工具开发。我写了一个代码来获得相同类型的页面,但它有很多错位。

from Tkinter import *
root = Tk()
root.title("Nokia Performance")
root.geometry("583x591+468+158")
root.title("NOKIA _ANSI Performance")
root.configure(borderwidth="1")
root.configure(relief="sunken")
root.configure(background="#dbd8d7")
root.configure(cursor="arrow")
root.configure(highlightbackground="#d9d9d9")
root.configure(highlightcolor="black")


Label1 = Label(root)
_img1 = PhotoImage(file="C:\\Users\\vkandhav\\Desktop\\PY_IMAGE\\NOKIA.gif")
Label1.configure(image=_img1)
Label1.configure(text='''Label''')
Label1.pack()

Label2 = Label(root)
Label2.configure(text='''Enter the platform :''')
Label2.pack(side=LEFT)

Entry2 = Entry(root)
Entry2.pack(side = RIGHT)

Label3 = Label(root)
Label3.configure(text='''Device  IP Address :''')
Label3.pack()

Entry5 = Entry(root)
Entry5.pack()

Label5 = Label(root)
Label5.configure(text='''Username :''')
Label5.pack()

Label6 = Label(root)
Label6.configure(text='''Label''')
Label6.pack()

Label7 = Label(root)
Label7.configure(text='''Craft IP :''')
Label7.pack()

Label8 = Label(root)
Label8.configure(text='''GICCI IP :''')
Label8.pack()

Label9 = Label(root)
Label9.configure(text='''Password :''')
Label9.pack()

Label10 = Label(root)
Label10.configure(text='''STC File Location''')
Label10.pack()

Label11 = Label(root)
Label11.configure(text='''STC Port :''')
Label1.pack()

Label12 = Label(root)
Label12.configure(text='''STC IP :''')
Label2.pack()


Entry6 = Entry(root)
Entry6.configure(background="white")
Entry6.configure(foreground="#000000")
Entry6.pack()

Entry7 = Entry(root)
Entry7.configure(background="white")
Entry7.pack()

Entry8 = Entry(root)
Entry8.configure(background="white")
Entry8.pack()

Entry9 = Entry(root)
Entry9.configure(background="white")
Entry9.configure(foreground="#000000")
Entry9.pack()

Entry10 = Entry(root)
Entry10.configure(background="white")
Entry10.configure(foreground="#000000")
Entry10.pack()

Entry11 = Entry(root)
Entry11.configure(background="white")
Entry11.configure(foreground="#000000")
Entry11.pack()

Radiobutton1 = Radiobutton(root)
Radiobutton1.configure(justify=LEFT)
Radiobutton1.configure(text='''NRNT-A''')
Radiobutton1.pack()

Radiobutton2 = Radiobutton(root)
Radiobutton2.configure(justify=LEFT)
Radiobutton2.configure(text='''SX-12VP''')
Radiobutton2.pack()

Radiobutton3 = Radiobutton(root)
Radiobutton3.configure(justify=LEFT)
Radiobutton3.configure(text='''DX-48''')
Radiobutton3.pack()

Radiobutton4 = Radiobutton(root)
Radiobutton4.configure(justify=LEFT)
Radiobutton4.configure(text='''SX-16VP''')
Radiobutton4.pack()

Radiobutton5 = Radiobutton(root)
Radiobutton5.configure(justify=LEFT)
Radiobutton5.configure(text='''SX-16F''')
Radiobutton5.pack()

Radiobutton6 = Radiobutton(root)
Radiobutton6.configure(justify=LEFT)
Radiobutton6.configure(text='''FX-8''')
Radiobutton6.pack()

Button1 = Button(root)
Button1.configure(pady="0")
Button1.configure(text='''NEXT''')
Button1.pack()
mainloop()

1 个答案:

答案 0 :(得分:2)

您正在创建一个小部件网格,因此我建议您使用grid。它记录良好且易于使用。

如果将所有布局代码组合在一起而不是将其与窗口小部件创建交织在一起,则这些类型的布局最简单。

我不打算改写你的整个程序,但这是一般的想法:

from Tkinter import *

root = Tk()
root.geometry("600x200")

root.grid_columnconfigure((0,1), weight=1)

Label3 = Label(root, text="Device IP Address")
Label4 = Label(root, text="Username")
Label5 = Label(root, text="Password")

Entry3 = Entry(root)
Entry4 = Entry(root)
Entry5 = Entry(root)

Label3.grid(row=3, column=0)
Entry3.grid(row=3, column=1, sticky="ew")
Label4.grid(row=4, column=0)
Entry4.grid(row=4, column=1, sticky="ew")
Label5.grid(row=5, column=0)
Entry5.grid(row=5, column=1, sticky="ew")

root.mainloop()

上面的代码创建了这个窗口:

screenshot