1

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)
asked Nov 21, 2020 at 13:11
1
  • What is to be printed? query_label ?? Commented Nov 21, 2020 at 13:44

1 Answer 1

1

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)

answered Nov 21, 2020 at 14:26
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for solving. It means a lot to me my dear friend.
You're welcome. Could you please then accept my answer as correct clicking on the tick on the left?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.