user14074784
user14074784

Reputation:

GUI-Tkinter-Python Program

How to create a button to open a new window in which all records are printed. In the following part of program, i am unable to print records in a new window, but it gets printed in the already created window & the new window remains empty. Here is that small part of program:-

    root = Tk()
    def query():
      query = Tk()
      query.title('Records')
      query.iconbitmap(r'C:\Users\pankaj\Downloads\Elegantthemes-Beautiful-Flat-Document.ico')
      query.geometry("450x350")

    #Create a database or connect to one
    conn = sqlite3.connect('Payslip.db')
    # Create cursor
    c = conn.cursor()
    #Query the database
    c.execute("SELECT *,oid from Payslip")
    records = c.fetchall()
    #print(records)# to print in the background

    #Loop the results
    print_records = ''
    for record in records: #to show the records
      print_records += str(record[0]) +"\t" + str(record[8])+ "\n"# \t to give space(tab) between them
    query_label = Label(root, text=print_records)
    query_label.grid(row=14, column=0, columnspan=2)

    #Commit Change
    conn.commit()
    # Close Connection
    conn.close()  
  
  #create a Query button
  query_btn = Button(root, text="Show Records", command=query)
  query_btn.grid(row=9,column=0, columnspan=2, pady=10, padx=10, ipadx=135)

Upvotes: 1

Views: 94

Answers (1)

Grinch
Grinch

Reputation: 179

Well, you are trying to print the print_records in the query_label, which you assigned to the root window:

query_label = Label(root, text=print_records)

You said you created a new window but I can't see it in the code, so you might want to do something like this:

def query():
    top1 = Toplevel() # creates new window called top1
    print_records = ''
    for record in records: #to show the records
        print_records += str(record[0]) +"\t" + str(record[8])+ "\n"
    query_label = Label(top1, text=print_records) # now the query_label is assigned to top1
    query_label.grid(row=14, column=0, columnspan=2)

However you want to do it:

query_label = Label(NEW_WINDOW, text=print_records)

Upvotes: 1

Related Questions