甚至可以在Tkinter中設置網格的絕對位置嗎?我正在嘗試創建一個類似於下圖的GUI,但我可能會以錯誤的方式進行操作。所以如果可能的話,你如何設置網格位置?如何使用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()
謝謝!我喜歡你如何解釋這些方法去做事情,以便我能夠理解如何徹底打破它。創建多個框架時,創建每個框架作爲方法還是作爲單獨的類創建更好?哪種更加Pythonic?謝謝。 – 2013-05-01 18:18:39