I have made a program where the user enter's a target number and 4 other numbers to make that target number. Right now I am having trouble to intergrate my tkinter interface to my code. So I am hoping that some one can help me
Tkinter interface:
window = Tk()
window.title("target number solution")
Label(window,image='', bg="white") .grid(row=0, column=0, sticky=W)
Label(window, text="Enter target number:",bg="black", fg="white", font="none 12 bold").grid(row=1, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=N)
Label(window, text="Enter first number:",bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=5, column=0, sticky=N)
Label(window, text="Enter second number:",bg="black", fg="white", font="none 12 bold").grid(row=6, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=7, column=0, sticky=N)
Label(window, text="Enter third number:",bg="black", fg="white", font="none 12 bold").grid(row=8, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=9, column=0, sticky=N)
Label(window, text="Enter fourth number:",bg="black", fg="white", font="none 12 bold").grid(row=10, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=11, column=0, sticky=N)
Button(window, text="Solve", width=6, command=solver).grid(row=12, column=0, sticky=N)
output = Text(window, width=60, height=10, wrap=WORD, background="white")
output.grid(row=13, column=0, columnspan=1, sticky=N)
window.mainloop()
This is the code that needs to be linked with the tkinter interface:
from itertools import permutations,combinations_with_replacement
numbers = []
target = int(input())
operators = ["+","-","*","/"]
groups = ['X+X+X+X', 'X+X+(X+X)', 'X+(X+X)+X', '(X+X+X)+X', '(X+X)+X+X', 'X+(X+X+X)', '((X+X)+X)+X', 'X+(X+(X+X))', 'X+((X+X)+X)', '(X+X)+(X+X)', '(X+(X+X))+X']
seen = set()
for values in permutations(numbers,len(numbers)):
for operCombo in combinations_with_replacement(operators,len(numbers)-1):
for oper in permutations(operCombo,len(numbers)-1):
formulaKey = "".join(oper+values)
if formulaKey in seen: continue # ignore variations on parentheses alone
for pattern in groups:
formula = "".join(o+p for o,p in zip([""]+list(oper), pattern.split("+")))
formula = "".join(v+p for v,p in zip([""]+list(values),formula.split("X")))
try:
if eval(formula) == target:
global Answer
Answer = formula,"=",target
print(formula,"=",target)
seen.add(formulaKey)
break
except: pass
All suggestions will be grealty appriciated
You can store the values for each number in seperate IntVar variables, then get the values of these variables inside the solver function and perform all the operations that are in your second code. The code should be modularised with the use of a class containing functions that create the widgets and run the solver code. To read more about class-based declarations in Tkinter, read this.
from tkinter import Tk, Frame, Label, Button, IntVar, Entry, Text, W, N, WORD, INSERT
from itertools import permutations,combinations_with_replacement
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.target_num = IntVar()
self.num1 = IntVar()
self.num2 = IntVar()
self.num3 = IntVar()
self.num4 = IntVar()
# self.title("target number solution")
Label(self,image='', bg="white").grid(row=0, column=0, sticky=W)
Label(self, text="Enter target number:", bg="black", fg="white", font="none 12 bold").grid(row=1, column=0, sticky=N)
self.textentry1 = Entry(self, textvariable=self.target_num, width=20, bg="white")
self.textentry1.grid(row=2, column=0, sticky=N)
Label(self, text="Enter first number:",bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=N)
self.textentry2 = Entry(self, textvariable=self.num1, width=20, bg="white")
self.textentry2.grid(row=5, column=0, sticky=N)
Label(self, text="Enter second number:",bg="black", fg="white", font="none 12 bold").grid(row=6, column=0, sticky=N)
self.textentry3 = Entry(self, textvariable=self.num2, width=20, bg="white")
self.textentry3.grid(row=7, column=0, sticky=N)
Label(self, text="Enter third number:",bg="black", fg="white", font="none 12 bold").grid(row=8, column=0, sticky=N)
self.textentry3 = Entry(self, textvariable=self.num3, width=20, bg="white")
self.textentry3.grid(row=9, column=0, sticky=N)
Label(self, text="Enter fourth number:",bg="black", fg="white", font="none 12 bold").grid(row=10, column=0, sticky=N)
self.textentry4 = Entry(self, textvariable=self.num4, width=20, bg="white")
self.textentry4.grid(row=11, column=0, sticky=N)
Button(self, text="Solve", width=6, command=self.solver).grid(row=12, column=0, sticky=N)
self.output = Text(self, width=60, height=10, wrap=WORD, background="white")
self.output.grid(row=13, column=0, columnspan=1, sticky=N)
def solver(self):
target = self.target_num.get()
number1 = self.num1.get()
number2 = self.num2.get()
number3 = self.num3.get()
number4 = self.num4.get()
numbers = [number1, number2, number3, number4]
operators = ["+","-","*","/"]
groups = ['X+X+X+X', 'X+X+(X+X)', 'X+(X+X)+X', '(X+X+X)+X', '(X+X)+X+X', 'X+(X+X+X)', '((X+X)+X)+X', 'X+(X+(X+X))', 'X+((X+X)+X)', '(X+X)+(X+X)', '(X+(X+X))+X']
seen = set()
for values in permutations(numbers,len(numbers)):
for operCombo in combinations_with_replacement(operators,len(numbers)-1):
for oper in permutations(operCombo,len(numbers)-1):
formulaKey = "".join(str(oper+values))
if formulaKey in seen: continue # ignore variations on parentheses alone
for pattern in groups:
formula = "".join(str(o)+str(p) for o,p in zip([""]+list(oper), pattern.split("+")))
formula = "".join(str(v)+str(p) for v,p in zip([""]+list(values),formula.split("X")))
try:
if eval(formula) == target:
Answer = formula,"=",target
print(formula,"=",target)
seen.add(formulaKey)
#insert value in output Textbox
self.output.insert(INSERT, Answer)
self.output.insert(END, '\n')
break
except: pass
root = Tk()
app = Application(master=root)
app.master.title("target number solution")
app.mainloop()
A few things that you can change/try:
Keep the "Entry" variables unique. Then extract each number using .get() command.
Use lambda in the command options of the Button as follows:
command = lambda: solver(num1, num2, num3, num4)
And if you want to display your returned number from 'solver', grid a Label in your tkinter window. Then use Label.config(text="Your number here") to show it in your tkinter window
Related
from tkinter import *
win=Tk()
var = StringVar()
l = Label(win, bg='white', width=15)
l.grid(row=17,column=1,padx=10, pady=10, sticky='w')
def print1_selection():
if var.get()=="Number":
lab1= Label(win, text="Enter a number").grid(row=4, column=0)
ent1=Entry(win).grid(row=4, column=1)
l.config(text='you have selected ' + var.get())
elif var.get()=="Alphabet":
lab21= Label(win, text="Enter an alphabet").grid(row=5, column=0)
ent21=Entry(win).grid(row=5, column=1)
l.config(text='you have selected ' + var.get())
lbl4=Label(win, text="Select One", bg="crimson", fg="white", font=("times new
roman",15,"bold")).grid(row=1, column=0, padx=10, pady=10, sticky='w')
r1 = Radiobutton(win, text='Number',variable=var, value='Number', command=print1_selection, width=22)
r1.grid(row=2,column=0,padx=10, pady=10)
r2 = Radiobutton(win, text='Alphabet', variable=var, value='Alphabet', command=print1_selection, width=22)
r2.grid(row=2,column=1,padx=10, pady=10)
win.mainloop()
In this code I want that when I select radiobutton number, only enter a number should appear and same for the other.
But the problem is that when I select number after selecting alphabet, it shows both. I need only the selected one and eliminate the other instantly.
This is how I would approach this issue:
from tkinter import Tk, StringVar, Label, Frame, Entry, Radiobutton
def print1_selection():
for widget in entry_frame.winfo_children():
widget.destroy()
value = var.get()
lbl.config(text='You have selected ' + value)
if value == "Number":
Label(entry_frame, text="Enter a number").grid(row=0, column=0)
Entry(entry_frame).grid(row=0, column=1)
elif value == "Alphabet":
Label(entry_frame, text="Enter an alphabet").grid(row=0, column=0)
Entry(entry_frame).grid(row=0, column=1)
win = Tk()
var = StringVar(value=0)
entry_frame = Frame(win)
entry_frame.grid(row=2, column=0, columnspan=2)
lbl = Label(win, bg='white', width=20)
lbl.grid(row=3, column=0, columnspan=2, padx=10, pady=10, sticky='w')
Label(win, text="Select One", bg="crimson", fg="white", font=("times new roman", 15, "bold")).grid(row=0, column=0, padx=10, pady=10, sticky='w')
Radiobutton(win, text='Number', variable=var, value='Number', command=print1_selection, width=22).grid(row=1, column=0, padx=10, pady=10)
Radiobutton(win, text='Alphabet', variable=var, value='Alphabet', command=print1_selection, width=22).grid(row=1, column=1, padx=10, pady=10)
win.mainloop()
As You can see if You don't plan on using the widgets instance anywhere You don't have to assign it to a variable. Also no need to configure label in both statements since that will be done anyways so just do it at the beginning, also rows start from 0 too. Frames help with organizing widgets. Also if You want neither of the radiobuttons selected set the variable to 0.
thanks for helping!
I have a spreadsheet that records my income and expenses, with dates, description, debit and credit amount. I wanted to used openpyxl to automate the process of adding values that are in a particular category, the program kind of works now, but the problem is: when I am searching for a phrase that don't exist in the sheet, the program crash, it can not do the things it suppose to before and after the search phrase that dont exist.
for example,
when i wanted to calculate the subtotal of search phrase "wage" and the put the subtotal into a target cell, it works fine. the problem comes out when i ask the program to look for something that isnt there.
I ask the program to look for wage, and create a subtotal, works fine and suppose to store the value at a defined target, when i ask the program to look for tax (which dont exist) the program showed nothing, then i ask the program to get the subtotal of rent (which exists). The program cant make the changes.
i am relatively new to all this.... so thanks again for helping! :)
from tkinter import *
import openpyxl as xl
window = Tk()
window.title("Excel Automation") # rename the title
def define_subtotal():
file_name = str(file_name_input_field.get()) # Collects the text from the text entry box
sheet_name = str(sheet_name_label_name_input_field.get()) # Collects the text from the text entry box
global wb # declare Workbook as global variable
wb = xl.load_workbook(file_name) # define workbook
sheet = wb[sheet_name] # Define sheet name
col_num = int(search_column_input_field.get())
search_phrase = search_phrase_input_field.get()
offset_col = int(offset_col_input_field.get())
target_col_num = int(target_col_input_field.get())
target_row_num = int(target_row_input_field.get())
total = 0
for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, col_num)
if cell.value is None:
continue
else:
if search_phrase.casefold() in str(cell.value).casefold():
total += cell.offset(column=offset_col).value
total_description = sheet.cell(target_row_num, target_col_num + 1)
total_description.value = "Subtotal of : " + search_phrase
total_cell = sheet.cell(target_row_num, target_col_num)
total_cell.value = total
output.delete(0.0, END)
output.insert(END, "Subtotal for " + search_phrase + " defined")
else:
continue
def execute():
output.delete(0.0, END) # clear the text box
new_file_name = new_file_input_field.get()
output.insert(END, "Calculations complete!")
wb.save(new_file_name + ".xlsx")
def import_excel_file():
output.delete(0.0, END) # clear the text box
output.insert(END, "File imported")
sheet_name_label_name_input_field.config (state='disabled')
import_button.config (state='disabled')
file_name_input_field.config (state='disabled')
def close_window(): # exit function
window.destroy()
exit()
### CONTENTS
file_name_label = Label(window, text="File Name:")
file_name_input_field = Entry(window, width=38, borderwidth=2)
sheet_name_label = Label(window, text="Sheet Name:")
sheet_name_label_name_input_field = Entry(window, width=38, borderwidth=2)
import_button = Button(window, text="Import", padx=35, pady=0, command=import_excel_file)
search_phrase_label = Label(window, text="Search Phrase:")
search_phrase_input_field = Entry(window, width=38, borderwidth=2)
search_column_label = Label(window, text="Search Column:")
search_column_input_field = Entry(window, width=38, borderwidth=2)
offset_col_label = Label(window, text="Offset Column:")
offset_col_input_field = Entry(window, width=38, borderwidth=2)
target_col_label = Label(window, text="Target Column:")
target_col_input_field = Entry(window, width=38, borderwidth=2)
target_row_label = Label(window, text="Target Row:")
target_row_input_field = Entry(window, width=38, borderwidth=2)
new_file_label = Label(window, text="Name of New file:")
new_file_input_field = Entry(window, width=38, borderwidth=2)
define_subtotal_button = Button(window, text="Define Subtotal", padx=5, pady=0, command=define_subtotal)
execute_button = Button(window, text="Execute", padx=5, pady=0, command=execute)
# contents Column 2
status_label = Label(window, text="Status:")
output = Text(window, width=50, height=25, wrap=WORD, bg="white") # wrap=WORD : wrap text when in overflow.
output.insert(END, "Drag and drop file into project file\n"
"Define File Name and Sheet Name\n"
"Example: filename.xlsx /.xlsm/ xltx/.xltm")
exit_button = Button(window, text="exit", width=14, command=close_window)
### THE GRID
file_name_label.grid(row=0, column=0, columnspan=2, padx=0, pady=0, sticky=W)
file_name_input_field.grid(row=1, column=0, columnspan=2, padx=5, pady=3, sticky=W)
sheet_name_label.grid(row=2, column=0, columnspan=2, padx=0, pady=0, sticky=W)
sheet_name_label_name_input_field.grid(row=3, column=0, columnspan=2, padx=5, pady=3, sticky=W)
import_button.grid(row=4, column=0, columnspan=2, sticky=W, padx=5)
search_phrase_label.grid(row=5, column=0, columnspan=2, padx=0, pady=0, sticky=W)
search_phrase_input_field.grid(row=6, column=0, columnspan=2, padx=5, pady=5, sticky=W)
search_column_label.grid(row=7, column=0, columnspan=2, padx=0, pady=0, sticky=W)
search_column_input_field.grid(row=8, column=0, columnspan=2, padx=5, pady=5, sticky=W)
offset_col_label.grid(row=9, column=0, columnspan=2, padx=0, pady=0, sticky=W)
offset_col_input_field.grid(row=10, column=0, columnspan=2, padx=5, pady=5, sticky=W)
target_col_label.grid(row=11, column=0, columnspan=2, padx=0, pady=0, sticky=W)
target_col_input_field.grid(row=12, column=0, columnspan=2, padx=5, pady=5, sticky=W)
target_row_label.grid(row=13, column=0, columnspan=2, padx=0, pady=0, sticky=W)
target_row_input_field.grid(row=14, column=0, columnspan=2, padx=5, pady=5, sticky=W)
new_file_label.grid(row=15, column=0, columnspan=2, padx=0, pady=0, sticky=W)
new_file_input_field.grid(row=16, column=0, columnspan=2, padx=5, pady=5, sticky=W)
define_subtotal_button.grid(row=17, column=0, sticky=W, padx=5)
# GRID column 1
execute_button.grid(row=17, column=1, sticky=W, padx=5)
# GRID Column 2
status_label.grid(row=0, column=2, padx=5, sticky=W)
output.grid(row=1, column=2, rowspan=25, padx=5, sticky=NE)
exit_button.grid(row=17, column=2, sticky=E)
window.mainloop()
[enter image description here][1]
may be it will be a good option to use
try:
your code
except:
pass
And run your code inside of this from which you want to skip the error with this try except. if any exception happened then it will ignore this and will be running.
I am creating a GUI that has two different types on entry for info. One is the checkbox and the other is the entry field. I want to have the show button display both inputs but I cannot seem to make it work in one button. I can change it to show one or the other. I am not sure what I am doing wrong to make both inputs display from the same button. Here is the code that I have. Right now it will display the entry field information, not the checkbox
import tkinter as tk
from tkinter import *
def show_entry_fields():
print("First Name: %s\nLast Name: %s\nPhone Number: %s"% (e1.get(),e2.get(), e3.get()))
master = tk.Tk()
master.title("Personal Info")
Label(master, text="Your gender:").grid(row=0,column=0, sticky=W)
var1 = IntVar()
Checkbutton(master, text="male", variable=var1).grid(row=0, column=1, sticky=W)
var2 = IntVar()
Checkbutton(master, text="female", variable=var2).grid(row=0, column = 2, sticky=W)
tk.Label(master, text="First Name").grid(row=1, pady=3)
tk.Label(master, text="Last Name").grid(row=2, pady=3)
tk.Label(master, text="Phone Number").grid(row=3, pady=3)
e1 = tk.Entry(master)
e2 = tk.Entry(master)
e3 = tk.Entry(master)
e1.grid(row=1, column=1, pady=3)
e2.grid(row=2, column=1, pady=3)
e3.grid(row=3, column=1, pady=3)
tk.Button(master, text='Quit', fg="red", command=master.quit).grid(row=4,column=0, sticky=tk.E, pady=10)
tk.Button(master, text='Show', fg="blue", command=show_entry_fields).grid(row=4, column=1, sticky=tk.W, padx=50, pady=10)
tk.mainloop()
if you want to get just one output( male or female ) which is the logic here with gender input .. then you have to use RadioButton not CheckButton
so your code will be like that:
import tkinter as tk
from tkinter import *
def show_entry_fields():
print("First Name: %s\nLast Name: %s\nPhone Number: %s\n gender: %s"% (e1.get(),e2.get(), e3.get(), var1.get()))
master = tk.Tk()
master.title("Personal Info")
Label(master, text="Your gender:").grid(row=0,column=0, sticky=W)
var1 = StringVar()
Radiobutton(master, text="male", variable=var1, value="male").grid(row=0, column=1, sticky=W)
Radiobutton(master, text="female", variable=var1, value="female").grid(row=0, column = 2, sticky=W)
tk.Label(master, text="First Name").grid(row=1, pady=3)
tk.Label(master, text="Last Name").grid(row=2, pady=3)
tk.Label(master, text="Phone Number").grid(row=3, pady=3)
e1 = tk.Entry(master)
e2 = tk.Entry(master)
e3 = tk.Entry(master)
e1.grid(row=1, column=1, pady=3)
e2.grid(row=2, column=1, pady=3)
e3.grid(row=3, column=1, pady=3)
tk.Button(master, text='Quit', fg="red", command=master.quit).grid(row=4,column=0, sticky=tk.E, pady=10)
tk.Button(master, text='Show', fg="blue", command=show_entry_fields).grid(row=4, column=1, sticky=tk.W, padx=50, pady=10)
tk.mainloop()
but let's say you have a list of options like preferred languages for example and you want to get the output of what exactly the user chooses .. in that case you should use ListBox
everything you need to know about ListBox here
So I'm trying to make this tkinter GUI in which you can add or search for a customer (the customer is stored in a SQlite database). Problem is, I can't see a way of making the def for when the add customer button is pressed, for as far as I know, I would need to use the names of the variables from lower down in the program where the widgets are created. I could switch the order of the functions around, but then the widgets would be calling functions that have not already been created. Can someone help fix this probem? Thanks in advance.
Here's the code:
from tkinter import *
app = Tk()
app.minsize(400,420)
app.title("Gatecode Manager")
app.resizable(False, False)
def add():
first = createWindow().firstAdd.get()
last = createWindow().firstAdd.get()
phone = createWindow().numberAdd.get()
def createWindow():
Label(text="Welcome to Gatecode Manager", font=("bold", 17)).grid(row=0, columnspan=2, pady=30, sticky=N)
Label(text="Search for a Customer").grid(row=1, column=0, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=0, padx=30, pady=10)
firstSearch = Entry().grid(row=3, column=0, padx=30)
Label(text="Last Name:").grid(row=4, column=0, padx=30, pady=10)
lastSearch = Entry().grid(row=5, column=0, padx=30)
Label(text="Add Customer to System").grid(row=1, column=1, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=1, padx=30, pady=10)
firstAdd = Entry().grid(row=3, column=1, padx=30)
Label(text="Last Name:").grid(row=4, column=1, padx=30, pady=10)
lastAdd = Entry().grid(row=5, column=1, padx=30)
Label(text="Phone Number:").grid(row=6, column=1, padx=30, pady=10)
numberAdd = Entry().grid(row=7, column=1, padx=30)
add = Button(text="Add", command="null").grid(padx=30, pady=10, column=1)
app.mainloop()
createWindow()
I find it difficult to understand exactly how you want this program to function. Is the add button supposed to call the function add() which then calls createWindow()?
I'm guessing a little here but what about:
1) Don't create the GUI in a function. A function will keep the variables in the scope of the functioin.
2) Use StringVar() to access entries. Since you don't save the identity of your entries it becomes impossible to interrogate them.
Example:
from tkinter import *
app = Tk()
app.minsize(400,420)
app.title("Gatecode Manager")
app.resizable(False, False)
def add():
first = firstAddVar.get()
last = firstAddVar.get()
phone = numberAddVar.get()
Label(text="Welcome to Gatecode Manager", font=("bold", 17)).grid(row=0,
columnspan=2, pady=30, sticky=N)
Label(text="Search for a Customer").grid(row=1, column=0, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=0, padx=30, pady=10)
firstSearchVar = StringVar()
firstSearch = Entry(textvar=firstSearchVar).grid(row=3, column=0, padx=30)
Label(text="Last Name:").grid(row=4, column=0, padx=30, pady=10)
lastSearchVar = StringVar()
lastSearch = Entry(textvar=lastSearchVar).grid(row=5, column=0, padx=30)
Label(text="Add Customer to System").grid(row=1, column=1, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=1, padx=30, pady=10)
firstAddVar = StringVar()
firstAdd = Entry(textvariable=firstAddVar).grid(row=3, column=1, padx=30)
Label(text="Last Name:").grid(row=4, column=1, padx=30, pady=10)
lastAddVar = StringVar()
lastAdd = Entry(textvariable=lastAddVar).grid(row=5, column=1, padx=30)
Label(text="Phone Number:").grid(row=6, column=1, padx=30, pady=10)
numberAddVar = StringVar()
numberAdd = Entry(textvariable=numberAddVar).grid(row=7, column=1, padx=30)
add = Button(text="Add", command=add).grid(padx=30, pady=10, column=1)
app.mainloop()
When your GUIs become more complex you should study OOP.
I am little bit comfused with grid system in tkinter Python. Can anyone show how to make it in right way?! ListBox and Label items positions are not in the places where I expexted to see them.
CODE:
self.third_label = Label(self, text="TEXT")
self.third_label.grid(row=2, column=0, columnspan=4, padx=10, pady=10, sticky=W)
self.fourth_label = Label(self, text="LONG TEXT")
self.fourth_label.grid(row=2, column=1, columnspan=4, padx=10, pady=10, sticky=W)
self.fifth_label = Label(self, text="SOME TEXT")
self.fifth_label.grid(row=2, column=2, columnspan=6, padx=10, pady=10, sticky=W)
self.sixth_label = Label(self, text="BIG TEXT")
self.sixth_label.grid(row=2, column=3, columnspan=4, padx=10, pady=10, sticky=W)
self.first_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.first_listbox.grid(row=3, column=0, columnspan=4, padx=10, pady=10, sticky=W)
self.second_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.second_listbox.grid(row=3, column=2, columnspan=4, padx=10, pady=10, sticky=W)
self.third_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.third_listbox.grid(row=3, column=4, columnspan=4, padx=10, pady=10, sticky=W)
self.fourth_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.fourth_listbox.grid(row=3, column=6, columnspan=4, padx=10, pady=10, sticky=W)
What I have right now:
Just Example:
The grid system works fine. The problem is your columnspans, which don't make much sense. You're gridding the widgets into certain column positions then giving them a columnspan that is beyond the range of where the next widget is to be gridded so on and so forth.
Small example:
import string
import tkinter as tk
root = tk.Tk()
for i in range(3):
tk.Label(root, text=string.ascii_letters).grid(row=0, column=i)
tk.Listbox(root, width=40).grid(row=1, column=i)
root.mainloop()
Edit from comments (for listbox size):
To get the number of lines in a listbox you can use the .size() method.
Image: