Posts: 8,165
Threads: 160
Joined: Sep 2016
Jan-21-2020, 08:56 PM
(This post was last modified: Jan-21-2020, 08:56 PM by buran.)
(Jan-21-2020, 08:48 PM)Melford Wrote: tkinter grid manager an install now you don't need to install anything.
the grid manager comes with tkinter
so you can use widget.grid() method where widget is the specific widget like Button that you wnat to place on the grid
use the b1.grid(...) like you did for the widgets before that
Posts: 8,165
Threads: 160
Joined: Sep 2016
before your next question - you don't want all your buttons to be called b1....
unless you store each of them in some sort of container like list before using the name b1 again for the next button
Posts: 18
Threads: 2
Joined: Jan 2020
Jan-21-2020, 09:03 PM
(This post was last modified: Jan-21-2020, 09:09 PM by Melford.)
just read your response give me a mo to check it
Oh okay, so by changing
b1 = Button(window, text="Search Entry", width=12)
b1 = grid(row=3, column=3)
b1 = Button(window, text="Add Entry", width=12)
b1 = grid(row=4, column=3)
b1 = Button(window, text="Update Selected", width=12)
b1 = grid(row=5, column=3)
b1 = Button(window, text="Delete Selected", width=12)
b1 = grid(row=6, column=3)
b1 = Button(window, text="Close", width=12)
b1 = grid(row=7, column=3) to
b1.Button(window, text="Search Entry", width=12)
b1.grid(row=3, column=3)
b1.Button(window, text="Add Entry", width=12)
b1.grid(row=4, column=3)
b1.Button(window, text="Update Selected", width=12)
b1.grid(row=5, column=3)
b1.Button(window, text="Delete Selected", width=12)
b1.grid(row=6, column=3)
b1.Button(window, text="Close", width=12)
b1.grid(row=7, column=3) I can then redefine
b1 = grid(row=2, column=3) to something like
b1 = window
b1 = b1.grid(row=2, column=3) Bit confused as I defined e1 as a button however, do I define the button after the button code?
I tried defining just B by itself to see if that would do it however it doesn't like that, would I need to define each button so b1 - b5?
Posts: 8,165
Threads: 160
Joined: Sep 2016
Jan-21-2020, 09:14 PM
(This post was last modified: Jan-21-2020, 09:14 PM by buran.)
from tkinter import *
# Create Window Object
window = Tk()
# Define table contents (each row/column)
l1 = Label(window, text="Name")
l1.grid(row=0, column=0)
l2 = Label(window, text="Monthly Income")
l2.grid(row=1, column=0)
l3 = Label(window, text="Monthly Budget")
l3.grid(row=2, column=0)
l4 = Label(window, text="Monthly Expenses")
l4.grid(row=3, column=0)
# Define Entries
name_text = StringVar()
e1 = Entry(window, textvariable=name_text)
e1.grid(row=0, column=1)
Monthly_Income_text = StringVar()
e2 = Entry(window, textvariable=Monthly_Income_text)
e2.grid(row=0, column=2)
Monthly_Budget = StringVar()
e3 = Entry(window, textvariable=Monthly_Budget)
e3.grid(row=0, column=3)
Monthly_Expenses = StringVar()
e4 = Entry(window, textvariable=Monthly_Expenses)
e4.grid(row=0, column=4)
# Define ListBox
list1 = Listbox(window, height=35, width=35)
list1.grid(row=2, column=0, columnspan=2)
# Attach scrollbar to the list
sb1 = Scrollbar(window)
sb1.grid(row=2, column=2, rowspan=6)
list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)
# Define buttons
b1 = Button(window, text="View All", width=12)
b1.grid(row=2, column=3)
b2 = Button(window, text="Search Entry", width=12)
b2.grid(row=3, column=3)
b3 = Button(window, text="Add Entry", width=12)
b3.grid(row=4, column=3)
b4 = Button(window, text="Update Selected", width=12)
b4.grid(row=5, column=3)
b5 = Button(window, text="Delete Selected", width=12)
b5.grid(row=6, column=3)
b6 = Button(window, text="Close", width=12)
b6.grid(row=7, column=3)
window.mainloop() that is what your gui looks like
Please note that this is based on your code, but in fact it can be written much better, e.g. iIt would be better to create using OOP approach.
I would recommend reading https://anzeljg.github.io/rin2/book2/240...index.html
IT IS VERY OUTDATED TUTORIAL and a lot has changed, but still it's one of the most comprehensive tutorials on tkinter
Posts: 18
Threads: 2
Joined: Jan 2020
Oh god, that looks horrible, I was hoping it'd looking more like this:
I'm assuming it might have something to do with:
list1 = Listbox(window, height=35, width=35) Is there any reason the boxes aren't stacking like the screenshot as I'm using it as a reference as its simple but something I (thought) could grasp? I think I might've miss tagged something so the boxes appear blank next to name?
Posts: 8,165
Threads: 160
Joined: Sep 2016
you need to be more carefull with the row and column properties of ech widget and in what order you add them.
Usually it takes some time to get accustom to placing the widgets. Also read the links I provided. They will help
I personally seldom do GUI
Posts: 18
Threads: 2
Joined: Jan 2020
Yeah I'll just fiddle around with the column and row numbers and see which fit where, do you think the width of the window will make a difference? Outside of that though thank you so much for your help you've been great once I've got this sorted im going to try integrate this and SQLite and I'll have the beginning of a very awful portfolio completed haha
Posts: 8,165
Threads: 160
Joined: Sep 2016
something like
from tkinter import *
# Create Window Object
window = Tk()
# Define table contents (each row/column)
l1 = Label(window, text="Name")
l1.grid(row=0, column=0)
# Define Entries
name_text = StringVar()
e1 = Entry(window, textvariable=name_text)
e1.grid(row=0, column=1)
l2 = Label(window, text="Monthly Income")
l2.grid(row=0, column=2)
Monthly_Income_text = StringVar()
e2 = Entry(window, textvariable=Monthly_Income_text)
e2.grid(row=0, column=3)
l3 = Label(window, text="Monthly Budget")
l3.grid(row=1, column=0)
Monthly_Budget = StringVar()
e3 = Entry(window, textvariable=Monthly_Budget)
e3.grid(row=1, column=1)
l4 = Label(window, text="Monthly Expenses")
l4.grid(row=1, column=2)
Monthly_Expenses = StringVar()
e4 = Entry(window, textvariable=Monthly_Expenses)
e4.grid(row=1, column=3)
# Define ListBox
list1 = Listbox(window)
list1.grid(row=3, column=0, rowspan=4, sticky=E+W)
# Attach scrollbar to the list
sb1 = Scrollbar(window)
sb1.grid(row=3, column=1, rowspan=4, sticky=N+S)
list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)
# Define buttons
b1 = Button(window, text="View All", width=12)
b1.grid(row=2, column=2, columnspan=2, sticky=N+E+S+W)
b2 = Button(window, text="Search Entry", width=12)
b2.grid(row=3, column=2, columnspan=2, sticky=N+E+S+W)
b3 = Button(window, text="Add Entry", width=12)
b3.grid(row=4, column=2, columnspan=2, sticky=N+E+S+W)
b4 = Button(window, text="Update Selected", width=12)
b4.grid(row=5, column=2, columnspan=2, sticky=N+E+S+W)
b5 = Button(window, text="Delete Selected", width=12)
b5.grid(row=6, column=2, columnspan=2, sticky=N+E+S+W)
b6 = Button(window, text="Close", width=12)
b6.grid(row=7, column=2, columnspan=2, sticky=N+E+S+W)
window.mainloop()
Far from good but better than the initial one. play with the grid manager to refine t
|