[英]GUI-Tkinter-Python 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)
那么,你要打印在query_label,您分配到根窗口print_records:
查詢標簽 = 標簽(根,文本=print_records)
你說你創建了一個新窗口,但我在代碼中看不到它,所以你可能想要做這樣的事情:
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)
但是你想這樣做:
query_label = Label( NEW_WINDOW , text=print_records)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.