GUITkinterPython程序

2024-05-29 02:48:09 发布

您现在位置:Python中文网/ 问答频道 /正文

如何创建一个按钮以打开一个打印所有记录的新窗口。 在程序的以下部分中,我无法在新窗口中打印记录,但它会在已创建的窗口中打印&;新窗口仍然是空的。 以下是该计划的一小部分:-

    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)

Tags: thetoconnectcreate记录rootrecordconn
1条回答
网友
1楼 · 发布于 2024-05-29 02:48:09

您正在尝试打印查询标签中的打印记录,您将其分配给窗口:

query\u label=label(root,text=print\u记录)

您说您创建了一个新窗口,但我在代码中看不到它,因此您可能希望执行以下操作:

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)

无论您想做什么:

查询标签=标签(新建窗口,文本=打印记录)

相关问题 更多 >

    热门问题