0

I am using Python 3.x, and I'm trying to fetch Dataset records and display them in an extra Window. I'm a bloody beginner and currently I don't see my error. I've checked multiple threads but didn't get the solution. Debugging isn't helping me either.

Here's my approach:

import sqlite3
from tkinter import *

class Overview():
    def __init__(self,master):
        self.master = master
        self.master.geometry('170x110+100+200')
        self.master.title('Welcome!')
        self.button1 = Button(self.master, text="Show dataset", fg='green', command=self.gotoMenu).grid(
            row=1, column=1)

    def gotoMenu(self):
        # This is the Dataset GUI#
        root2 = Toplevel(self.master)
        myGUI = Menu(root2)

def main():
        root = Tk()
        overviewGUI = Overview(root)
        root.mainloop()

if __name__ == '__main__':
     main()


class Menu:
    def __init__(self,master):
        # This is the Dataset GUI#

        self.connection = sqlite3.connect('test.db')
        print("Opened database successfully")
        self.cur = self.connection.cursor()

        self.master = master
        self.master.title('Dataset')
        self.master.geometry("320x240")
        print("GUI created")
        self.dateLabel = Label(self.master, text="Date", width=10)
        self.dateLabel.grid(row=0, column=0)
        self.BMILabel = Label(self.master, text="Name", width=10)
        self.BMILabel.grid(row=0, column=1)
        self.stateLabel = Label(self.master, text="ID", width=10)
        self.stateLabel.grid(row=0, column=2)
        self.insertDS('random')
        self.showallrecords()

    def showallrecords(self):
        data = self.readfromdatabase()
        for index, dat in enumerate(data):
            Label(self.master, text=dat[2]).grid(row=index + 1, column=0)
            Label(self.master, text=dat[1]).grid(row=index + 1, column=1)
            Label(self.master, text=dat[0]).grid(row=index + 1, column=2)

    def readfromdatabase(self):
        self.cur.execute("SELECT * FROM LOGGING")
        return self.cur.fetchall()

    def createTable(self):
        try:
            self.connection.execute(
                "CREATE TABLE LOGGING(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT NOT NULL, TIMESTAMP DATE DEFAULT (datetime('now','localtime')));")
            print("Table created successfully")

        except:
            print("Table already exists")

    def insertDS(self, name):
        self.connection.execute("INSERT INTO LOGGING (NAME) \
            VALUES (?);", [name])
        self.connection.commit()
        print("Records created successfully")

My Application should start with the "Overview" GUI, on button click I want to see all fetched Datasets from "Menu" Class.

However, after clicking the button the next window is empty and has the title "Welcome" which should be "Dataset"

UPDATE:

Example

I am getting no error, BUT my fetched results won't show in the 2nd Window, and the title isn't self.master.title('Dataset') as initialized in class Menu.

I feel like it's just creating some empty Window without even looking in my Menu Class?

Solution:

python className not defined NameError

Somehow(can't explain why) when I moved the ´name´ block down with the def (even though indentions were right) it worked.

Hendrik
  • 310
  • 6
  • 19
  • Is there any error? `Toplevel` window is empty and with parent's title because this `root2 = Toplevel(self.master)` line is succesfull, but after that there's some error in `Menu` with connection. I tried your code w/o a `sqlite3` stuff and [it's seems to work](https://i.stack.imgur.com/mYItk.png)! – CommonSense May 23 '17 at 20:23
  • Edited, hope it's more clearly now PS: What did you change that you see the labels? – Hendrik May 23 '17 at 20:28
  • https://stackoverflow.com/questions/2386714/why-is-import-bad Menu is a tkinter class, now you see the problem I think. – brodeur May 23 '17 at 21:08
  • @brodeur Can you explain further? – Hendrik May 23 '17 at 21:09

1 Answers1

0

You have overloaded Menu, change your class name by an another name. And don't use import *

brodeur
  • 96
  • 5
  • Exception in Tkinter callback Traceback (most recent call last): line 15, in gotoMenu myGUI = Dataset(root2) NameError: name 'Dataset' is not defined Changed the class name from Menu to `class Dataset():` – Hendrik May 23 '17 at 21:17