简体   繁体   中英

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM