每隔一段时间查询一次数据库信息并返回listbox中进行展示
import tkinter as tk
from tkinter import ttk, messagebox, simpledialog
import os
import cx_Oracle
import time
import threading
class MemoApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("数据库监控工具")
self.geometry("400x300")
self.resizable(False, False)
#self.attributes("-alpha", 0.7)
# 创建笔记本卡片
self.notebook = ttk.Notebook(self)
self.notebook.pack(expand=True, fill="both")
# 创建查询卡片
self.cx1 = tk.Frame(self.notebook)
self.notebook.add(self.cx1, text="数据库查询")
self.cx1_list = tk.Listbox(self.cx1)
self.cx1_list.pack(expand=True, fill="both")
# 创建查询卡片
self.cx2 = tk.Frame(self.notebook)
self.notebook.add(self.cx2, text="数据库查询")
self.cx2_list = tk.Listbox(self.cx2)
self.cx2_list.pack(expand=True, fill="both")
# 创建查询卡片
self.cx3 = tk.Frame(self.notebook)
self.notebook.add(self.cx3, text="数据库查询")
self.cx3_list = tk.Listbox(self.cx3)
self.cx3_list.pack(expand=True, fill="both")
self.thread = threading.Thread(target=self.Oracle_link())
self.thread.start()
def Oracle_link(self):
conn = cx_Oracle.connect('') # 这里的顺序是用户名/密码@oracleserver的ip地址/数据库名字
cursor = conn.cursor()
print('连接数据库成功!')
self.cx1_list.delete(0, tk.END)
self.cx2_list.delete(0, tk.END)
self.cx3_list.delete(0, tk.END)
sql1 = "select ywh from testtable1"
all1 = cursor.execute(sql1)
#print(all.fetchall())
i = 1
for line in all1.fetchall():
line = str(i) + "、"+ line[0]
print(line)
i=i+1
self.cx1_list.insert(tk.END, line.strip()) # strip()
sql2 = "select ywh from testtable1"
all2 = cursor.execute(sql2)
# print(all.fetchall())
i = 1
for line in all2.fetchall():
line = str(i) + "、" + line[0]
print(line)
i = i + 1
self.cx2_list.insert(tk.END, line.strip()) # strip()
sql3 = "select ywh from testtable1"
all3 = cursor.execute(sql3)
# print(all.fetchall())
i = 1
for line in all3.fetchall():
line = str(i) + "、" + line[0]
print(line)
i = i + 1
self.cx3_list.insert(tk.END, line.strip()) # strip()
self.after(5000, self.Oracle_link)
# 主程序入口
if __name__ == "__main__":
app = MemoApp()
app.mainloop()
效果图如下:
|