甚至可以在 Tkinter 中设置网格的绝对位置吗?我正在尝试创建一个类似于下面的 GUI,但我可能会以错误的方式进行操作。那么如果可能的话,如何设置网格位置呢?
目标图形用户界面:
到目前为止,这就是我的 GUI 的结果:
如您所见,我的新联系人需要在右侧,而联系人列表应该在左侧。我了解如何使用绝对值移动联系人列表,但我可以为我的网格元素做同样的事情吗?或者我应该对所有这些都使用绝对值,并结合填充?
目前,这是我的代码:
from tkinter import *
contacts=['Justin Day']
class Contact_manager (Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Contact Manager")
self.pack(fill=BOTH, expand=1)
#New contact grid
Label (self, text = "New Contact").grid (row=0, columnspan=2)
Label (self, text = "First Name:").grid (row=1, sticky=E)
Label (self, text = "Last Name:").grid (row=2, sticky=E)
Label (self, text = "Phone#").grid (row=3, sticky=E)
self.entry1 = Entry(self)
self.entry2 = Entry(self)
self.entry3 = Entry(self)
self.entry1.grid (row=1, column=1)
self.entry2.grid (row=2, column=1)
self.entry3.grid (row=3, column=1)
friend_check = IntVar()
self.friend_check = Checkbutton (self, variable = friend_check,
command = self.friend_box,
text = "Friend")
self.friend_check.grid (row=4, columnspan=2)
Label (self, text = "Email:").grid (row=5, sticky=E)
Label (self, text = "Birthday:").grid (row=6, sticky=E)
self.entry4 = Entry(self)
self.entry5 = Entry(self)
self.entry4.grid (row=5, column=1)
self.entry5.grid (row=6, column=1)
#Contact listbox
Label (self, text = "Contact List").place(x=20, y=190)
contact_lb = Listbox(self)
for i in contacts:
contact_lb.insert(END, i)
contact_lb.bind("<<ListboxSelect>>", self.onSelect)
contact_lb.place(x=20, y=210)
def onSelect(self, val):
sender = val.widget
idk = sender.curselection()
value = sender.get(idx)
self.var.set(value)
def friend_box():
if friend_check.get() == 1:
contacts.append(Friend(f, l, p, e, bd))
else:
contacts.append(Person(f, l, p))
def main():
root = Tk()
ex = Contact_manager(root)
root.geometry('600x700+200+100')
root.mainloop()
if __name__ == '__main__':
main()