alex deralu
alex deralu

Reputation: 589

How to display 2d list as a table in tkinter on a new window?

I am trying to display a 2d list as CSV like structure in a new tkinter window

here is my code

import tkinter as tk
import requests
import pandas as pd
import numpy as np
from tkinter import messagebox
from finta import TA
from math import exp
import xlsxwriter
from tkinter import *
from tkinter.ttk import *
import yfinance as yf
import csv
from operator import itemgetter

def Createtable(root,rows):
        # code for creating table
        newWindow = Toplevel(root)
        newWindow.title("New Window")
     
        # sets the geometry of toplevel
        newWindow.geometry("600x600")

        for i in range(len(rows)):
            for j in range(3):
                  
                self.e = Entry(root, width=20, fg='blue',
                               font=('Arial',16,'bold'))
                  
                self.e.grid(row=i, column=j)
                self.e.insert(END, rows[i][j])

        Label(newWindow, text ="Results").pack()

This is my code for display the 2d list in a new window, here rows is the 2d list

This is how I am calling the function

def clicked():
    selected_stocks=[]
    converted_tickers=[]
    selected = box.curselection()
    for idx in selected:
        selected_stocks.append(box.get(idx))
    for i in selected_stocks:
        converted_tickers.append(name_to_ticker[i])
    rows=compute(converted_tickers)
    Createtable(app,rows)


btn = Button(app, text = 'Submit',command = clicked)
btn.pack(side = 'bottom')

rows works, I printed it out seperately and confirmed.

When I execute the program this is the error I receive

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
    return self.func(*args)
  File "pair.py", line 144, in clicked
    Createtable(app,rows)
  File "pair.py", line 31, in Createtable
    self.e = Entry(root, width=20, fg='blue',
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/tkinter/ttk.py", line 669, in __init__
    Widget.__init__(self, master, widget or "ttk::entry", kw)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/tkinter/ttk.py", line 557, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 2567, in __init__
    self.tk.call(
_tkinter.TclError: unknown option "-fg"

I looked up the error and it says that this happens when you are using both old and new version of tkinter. I am not able to understand this as I am not using tk and ttk seperately anywhere in the Createtable function.

How can I solve this issue to display the 2d list on the new window?

For a reference I am attaching a screenshot here

enter image description here

UPDATE: Updated code as per comment

1)I commented out from tkinter.ttk import * 2)I also changed the Createtable function into a class like this

class Table:
      
    def __init__(self,root,rows):
        # code for creating table
        newWindow = Toplevel(root)
        newWindow.title("New Window")
     
        # sets the geometry of toplevel
        newWindow.geometry("600x600")

        for i in range(len(rows)):
            for j in range(3):
                  
                self.e=Entry(root, width=20, fg='blue',
                               font=('Arial',16,'bold'))
                  
                self.e.grid(row=i, column=j)
                self.e.insert(END, rows[i][j])

        Label(newWindow, text ="Results").pack()

Now, two errors are happening

1.The 2d list is showing on top of the main window instead of the new window.

2.After removing ttk the words on top of the button have become white for some reason. You can see in the picture attached below ( compare with the old picture, you will see "Submit" has become white)

How do I solve these?

Attaching picture for reference enter image description here

UPDATE 2: minimum reproducible code

import tkinter as tk
import requests
import pandas as pd
import numpy as np
from tkinter import messagebox
from finta import TA
from math import exp
import xlsxwriter
from tkinter import *
import tkinter.ttk as ttk
import yfinance as yf
import csv
from operator import itemgetter



class Table:
      
    def __init__(self,root,rows):
        # code for creating table
        newWindow = Toplevel(root)
        newWindow.title("New Window")
        # sets the geometry of toplevel
        newWindow.geometry("600x600")

        for i in range(len(rows)):
            for j in range(3):
                self.e=Entry(newWindow)
                  
                self.e.grid(row=i, column=j)
                self.e.insert(END, rows[i][j])

        Label(newWindow, text ="Results").pack()

#######################LIST TO DISPLAY##############

final_sorted_list=['Allahabad Bank', 'Andhra Bank', 'Axis Bank Limited','Bank of Baroda','Bank of India Limited']




########TKINTER##############################

app = Tk()
app.title('Test')
app.geometry("500x800")



def clicked():
    
    rows=[['Stock 1', 'Stock 2', 'Value'], ['AXISBANK.NS', 'MAHABANK.NS', 81.10000000000001], ['AXISBANK.NS', 'BANKINDIA.NS', 82.3], ['BANKBARODA.NS', 'MAHABANK.NS', 84.8], ['MAHABANK.NS', 'CANBK.NS', 85.5], ['BANKBARODA.NS', 'BANKINDIA.NS', 90.4], ['BANKINDIA.NS', 'CANBK.NS', 90.9], ['AXISBANK.NS', 'CANBK.NS', 91.5], ['AXISBANK.NS', 'BANKBARODA.NS', 93.30000000000001], ['BANKINDIA.NS', 'MAHABANK.NS', 95.8], ['BANKBARODA.NS', 'CANBK.NS', 97.6]]
    Table(app,rows)
    print("Finished")


box = Listbox(app, selectmode=MULTIPLE, height=4)
for val in final_sorted_list:
    box.insert(tk.END, val)
box.pack(padx=5,pady=8,side=LEFT,fill=BOTH,expand=True)

scrollbar = Scrollbar(app)
scrollbar.pack(side = RIGHT, fill = BOTH)

box.config(yscrollcommand = scrollbar.set)
# scrollbar.config(command = box.yview)

btn = ttk.Button(app, text = 'Submit',command = clicked)
btn.pack(side = 'bottom')

exit_button = ttk.Button(app, text='Close', command=app.destroy)
exit_button.pack(side='top')

clear_button = ttk.Button(app, text='Clear Selection',command=lambda: box.selection_clear(0, 'end'))
clear_button.pack(side='top')



app.mainloop()


If you run this, you get a frontend like in the picture above. You don't need to select anything as a sample result already has been hard coded in rows. The data in rows needs to be displayed in another window as a table. To recreate the error (new window not popping up) - you can just click on the "Submit" button, you don't need to select anything from the list.

Upvotes: 1

Views: 1270

Answers (2)

Henry
Henry

Reputation: 3942

The problem update 2 is that you are using pack and grid in the same window. When I click submit the new window comes up (I couldn't reproduce that problem) but the word "results" is missing. This is because you are trying to use pack and grid on the same window. To fix this, show "results" using grid like this:

class Table:
      
    def __init__(self,root,rows):
        # code for creating table
        newWindow = Toplevel(root)
        newWindow.title("New Window")
        # sets the geometry of toplevel
        newWindow.geometry("600x600")
        Label(newWindow, text ="Results").grid(row = 0, column = 0, columnspan = 3)
        for i in range(len(rows)):
            for j in range(3):
                self.e=Entry(newWindow)
                  
                self.e.grid(row=i+1, column=j)
                self.e.insert(END, rows[i][j])

Because the "results" label is now in column 1 I've had to change self.e.grid to use row=i+1 so it displays properly. I also set the columnspan of the "results" label to 3 so it will be centered.

Upvotes: 1

Derek
Derek

Reputation: 2234

I've organized your imports. This should remove naming conflicts.

Note: You will have to declare tkinter objects with tk. and ttk objects with ttk. This should also help to remove naming conflicts.


import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

import requests
import csv
import pandas as pd
import numpy as np
import xlsxwriter
import yfinance as yf

from finta import TA
from math import exp
from operator import itemgetter

Upvotes: 0

Related Questions