Pysimplegui: [Addressed]Displaying records from a MySQL db to sg.Table()

Created on 7 Jan 2019  Â·  10Comments  Â·  Source: PySimpleGUI/PySimpleGUI

Is this possible with PySimpleGUI? If yes, how is it properly done?
I tried

mycursor.execute("SELECT * FROM thesisdb.users")

rec = mycursor.fetchall()

usersTab = [[sg.Button(key='Add User', button_color=sg.TRANSPARENT_BUTTON, border_width=0, image_filename='Buttons/Add.png')],
                    [sg.Table(values=rec)]]
logsTab = [[sg.T('Test Logs')]]

adminMainLayout = [[sg.TabGroup([[sg.Tab('Users', usersTab), sg.Tab('Logs', logsTab)]])]]

But it gives an error saying "TypeError: 'NoneType' object is not iterable"

All 10 comments

No, it doesn't work that way.

You need to build a window with a Table Element inside.

There are Demo Programs that show you how to work with tables. I suggest starting there and reading the first few section of the documentation (http://www.PySimpleGUI.org)

Sure it is possible. I have made something based on SQLite, but the
principle is the same.
You need a library like pymysql

Op ma 7 jan. 2019 om 15:57 schreef OndoyManing notifications@github.com:

Is this possible with PySimpleGUI?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/PySimpleGUI/PySimpleGUI/issues/1048, or mute the
thread
https://github.com/notifications/unsubscribe-auth/AFkYZ282TdRAwWUCJ0-ETfvb7C1wIDeUks5vA2B3gaJpZM4ZzpZ-
.

Woops! Sorry about that, didn't provide that much code. It's actually inside a tab layout.

Edited first post

The None Type error is one we see when the syntax is not correct. Usually means you've got the wrong number of [ ] in the layout.

When I ran your code I got a None Type error when the table code tried to create the table. This particular error was due to no column headings being defined. You need to add to your Table call the headings.

None Type error is now gone but a different set of errors have appeared.

Traceback (most recent call last):
File "C:/Users/Necy/Desktop/Python Projects/MessageBox/test.py", line 238, in
main()
File "C:/Users/Necy/Desktop/Python Projects/MessageBox/test.py", line 154, in main
event5, values5 = adminMainWin.Read(timeout=0)
File "C:Users\Necy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 3560, in Read
event, values = self.ReadNonBlocking()
File "C:Users\Necy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 3664, in ReadNonBlocking
self.Show(non_blocking=True)
File "C:Users\Necy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 3498, in Show
StartupTK(self)
File "C:Users\Necy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 5861, in StartupTK
ConvertFlexToTK(my_flex_form)
File "C:Users\Necy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 5778, in ConvertFlexToTK
PackFormIntoFrame(MyFlexForm, master, MyFlexForm)
File "C:Users\Necy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 5495, in PackFormIntoFrame
PackFormIntoFrame(element, toplevel_form.TKroot, toplevel_form)
File "C:Users\Necy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 5427, in PackFormIntoFrame
PackFormIntoFrame(element, element.TKFrame, toplevel_form)
File "C:Users\Necy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 5586, in PackFormIntoFrame
width = max(column_widths[i], len(heading))
KeyError: 4

Did you take a look at the Demo programs I suggested that show the correct use of the Table Element?

There are a large number of parameters.

You'll find the basic program here:
https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Table_Element.py

The error you're getting is dealing with the column sizes and headings. Please check to be sure you've go the right number of headings specified.

Have you printed the data you're passing in to make sure it's a correctly formatted table?

It's behaving as if you're got a bad table.

I suggest making a simpler window that only has the table element, not embedded inside of a tab. Get that working and then put it into a more complex window.

Are you running PySimpleGUI or PySimpleGUIQt?

After looking at your error more, it appears as if the number of column headings that was specified didn't match the number of columns in the table.

import PySimpleGUI as sg

values = [['1','2','3'],['4','5','6']]
header = values[0]
usersTab = [[sg.Button('     ', key='Add User', button_color=sg.TRANSPARENT_BUTTON, border_width=0, )],
                    [sg.Table(values=values,auto_size_columns=False, headings=header)]]
logsTab = [[sg.T('Test Logs')]]

adminMainLayout = [[sg.TabGroup([[sg.Tab('Users', usersTab), sg.Tab('Logs', logsTab)]])]]

layout = adminMainLayout

window = sg.Window('My window', border_depth=0).Layout(layout)

window.Read()

After looking at your error more, it appears as if the number of column headings that was specified didn't match the number of columns in the table.

import PySimpleGUI as sg

values = [['1','2','3'],['4','5','6']]
header = values[0]
usersTab = [[sg.Button('     ', key='Add User', button_color=sg.TRANSPARENT_BUTTON, border_width=0, )],
                    [sg.Table(values=values,auto_size_columns=False, headings=header)]]
logsTab = [[sg.T('Test Logs')]]

adminMainLayout = [[sg.TabGroup([[sg.Tab('Users', usersTab), sg.Tab('Logs', logsTab)]])]]

layout = adminMainLayout

window = sg.Window('My window', border_depth=0).Layout(layout)

window.Read()

You are indeed right! It is up and running. I was probably way too sleepy to check my table structure—it was 3 in the morning when I stumbled into that mess.
https://imgur.com/vHaTaWc

However, adding a new user does not update the table.
https://imgur.com/h87sUCL
I tried creating a method for the layout of the admin window so I could just call it when I press the back button in the hopes of refreshing the table, but it doesn't refresh the table.

def adminlayout():
    mycursor = mydb.cursor()
    mycursor.execute("SELECT * FROM thesisdb.users")
    data = mycursor.fetchall()
    headings = ['User ID', 'First Name', 'Last Name', 'Username', 'Password', 'Role']
    usersTab = [[sg.Button(key='Add User', button_color=sg.TRANSPARENT_BUTTON, border_width=0,
                           image_filename='Buttons/Add.png')],
                [sg.Table(values=data, headings=headings)]]
    logsTab = [[sg.T('Test Logs')]]
    adminMainLayout = [[sg.Button(key='Back', button_color=sg.TRANSPARENT_BUTTON, border_width=0, image_filename='Buttons/Back.png')],
                    [sg.TabGroup([[sg.Tab('Users', usersTab), sg.Tab('Logs', logsTab)]])]]

    adminMainWin = sg.Window('Admin Main Window', size=(700, 500)).Layout(
        adminMainLayout)
    return adminMainWin

Calling the admin layout:

  if event6 == 'Back' or event6 is None:
                addUserWin_active: False
                addUserWin.Close()
                adminMainWin_active: True
                adminMainWin = adminlayout()

Any help is highly appreciated.

EDIT
Managed to fix the table contents not refreshing by creating a connection inside the adminlayout() method instead of having it outside.

I get nervous anytime people use Tables and Tabs. The code is some of the trickiest and brittle of the package. I would like to get the colors working, but struggle with tkinter styles.

Good to hear you've made progress!

Was this page helpful?
0 / 5 - 0 ratings