Reputation: 41
I write labels and entries inside notebook tab, however it doesn't lined up vertically. Instead, it's horizontal.
I tried using grid, but it doesn't work because:
_tkinter.TclError: cannot use geometry manager grid inside .!notebook.!frame which already has slaves managed by pack
from tkinter import *
from tkinter import ttk
class View:
def __init__(self, root):
# Define software title
root.title("Plotting Management System")
# Define software's width and height
root.geometry("800x600")
# Define label
L0 = Label(root, text="Plotting Management System", font="Helvetica 24 bold")
L0.grid(row=0)
L0.pack(anchor="nw")
tab_parent = ttk.Notebook(root)
tab0 = ttk.Frame(tab_parent)
tree0 = ttk.Treeview(tab0)
tree0["columns"] = ("one", "two", "three", "four")
tree0.heading("#0", text="Kode Dosen", anchor="w")
tree0.column("#0", minwidth=0, width=100, stretch=NO)
tree0.heading("one", text="Nama Dosen", anchor="w")
tree0.column("one", minwidth=0, width=300, stretch=NO)
tree0.heading("two", text="JFA", anchor="w")
tree0.column("two", minwidth=0, width=100, stretch=NO)
tree0.heading("three", text="Prodi", anchor="w")
tree0.column("three", minwidth=0, width=80, stretch=NO)
tree0.heading("four", text="KK", anchor="w")
tree0.column("four", minwidth=0, width=80, stretch=NO)
tree0.pack(anchor="w")
L1 = Label(tab0, text="Kode Dosen")
L1.pack(side="left", anchor="w")
E1 = Entry(tab0, width=50)
E1.pack(side="left")
L2 = Label(tab0, text="Nama Dosen")
L2.pack(side="left", anchor="w")
E2 = Entry(tab0, width=50)
E2.pack(side="left")
L3 = Label(tab0, text="JFA")
L3.pack(side="left", anchor="w")
E3 = Entry(tab0, width=50)
E3.pack(side="left")
L4 = Label(tab0, text="Prodi")
L4.pack(side="left", anchor="w")
E4 = Entry(tab0, width=50)
E4.pack(side="left")
L5 = Label(tab0, text="KK")
L5.pack(side="left", anchor="w")
E5 = Entry(tab0, width=50)
E5.pack(side="left")
tab_parent.add(tab0, text="Data Dosen")
tab_parent.pack(fill="both")
if __name__ == "__main__":
root = Tk()
gui = View(root)
root.mainloop()
I want them to lined up vertically, not horizontally.
Wanted Result:
Label 1 | Entry 1
Label 2 | Entry 2
Label 3 | Entry 3
Actual Result:
Label 1 | Entry 1 | Label 2 | Entry 2 | ...
Upvotes: 0
Views: 63
Reputation: 22503
As the error shows, you cannot mix grid
and pack
with the same parent. But you can add another frame using pack
, and have your Label
and Entry
widgets using grid
as the geometry manager inside that frame.
class View:
def __init__(self, root):
...
label_frame = ttk.Frame(tab0)
label_frame.pack(anchor="w")
L1 = Label(label_frame, text="Kode Dosen")
L1.grid(row=0,column=0)
E1 = Entry(label_frame, width=50)
E1.grid(row=0,column=1)
...
Also I recommend using a for loop to create your Label/Entry pairs. It can save you from repetitive codes:
for num, i in enumerate(("Kode Dosen","Nama Dosen","JFA","Prodi","KK")):
a = Label(label_frame, text=i)
a.grid(row=num, column=0)
b = Entry(label_frame, width=50)
b.grid(row=num,column=1)
If you need the value of the Entry
, you can append them to a list during the loop.
Upvotes: 2