这是我在中科院写的第一个python程序,导师让我统计关于病毒和细菌数据库的信息,我决定趁此机会学习python,并写一个GUI程序,读取我的信息列表。
把代码放在下面,不违反我们所的保密协议吧,哈哈。涉及的东西有tk的label,text,scrollbar,访问数据库这些内容
#copyright @ ChenSun Beijing Institute of Genomics, Chinese Academy of Sciences 2009
from Tkinter import *
root = Tk()
#read the database
import win32com.client
conn = win32com.client.Dispatch('ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=E:/biology/BS(Influenza Database)/database_analysis/database_of_bio_databases.mdb'
conn.Open(DSN)
rs = win32com.client.Dispatch('ADODB.Recordset')
rs_name = 'microbo' #table name
rs.Open('['+ rs_name +']', conn, 1, 3)
flds = {}
class DataReader:
def __init__(self, master):
#database name
self.label_name = Label(master, name = "label_name", text = "Database Name")
self.label_name.place(x = 5, y = 5)
self.text_name = Text(height = 1)
self.text_name.place(x = 105, y = 5, width = 600)
self.text_name.insert('1.0', 'text_name')
#About
self.label_about = Label(master, text = "About")
self.label_about.place(x = 5, y = 30)
self.text_about = Text(master, height = 1)
self.text_about.place(x = 105, y = 30, width = 600)
self.text_about.insert('1.0', "Virus")
#amount of data
self.label_num = Label(master, text = "Amount of Data")
self.label_num.place(x = 5, y = 55)
self.text_num = Text(master, height = 2)
self.text_num.place(x = 105, y = 55, width = 600)
self.text_num.insert('1.0', "text_num")
#introduction of database
self.label_intro = Label(master, text = "Introduction")
self.label_intro.place(x = 5, y = 100)
self.text_intro = Text(master)
self.text_intro.place(x = 105, y = 100, width = 600, height = 200)
self.scrollbar_intro = Scrollbar(master, orient = VERTICAL, command = self.text_intro.yview)
self.scrollbar_intro.place(x = 705, y = 100, height = 200)
self.text_intro["yscrollcommand"] = self.scrollbar_intro.set
#tools of database
self.label_tools = Label(master, text = "Tools")
self.label_tools.place(x = 5, y = 305)
self.text_tools = Text(master)
self.text_tools.place(x = 105, y = 305, width = 600, height = 250)
self.scrollbar_tools = Scrollbar(master, orient = VERTICAL, command = self.text_tools.yview)
self.scrollbar_tools.place(x = 705, y = 305, height = 250)
self.text_tools["yscrollcommand"] = self.scrollbar_tools.set
#button
self.button_forward = Button(master, text = ">>", command = self.Forward)
self.button_forward.place(x = 710, y = 570, height = 20, width = 40)
self.button_backword = Button(master, text = "<<", command = self.Backward)
self.button_backword.place(x =660, y = 570, height = 20, width = 40)
#copyright @ ChenSun Beijing Institute of Genomics, Chinese Academy of Sciences
self.label_copyright = Label(master, text = "Copyright(c) ChenSun Beijing Institute of Genomics, Chinese Academy of Sciences 2009")
self.label_copyright.place(x = 5, y = 570)
rs.MoveFirst()
self.WriteData(rs, flds)
def WriteData(self, rs, flds):
for x in range(rs.Fields.Count):
flds[x] = rs.Fields.Item(x).Value
if flds[x] == 'no' or flds[x] == None:
flds[x] = 'Unclassified Statistical Data'
self.text_name.delete('1.0', END)
self.text_name.insert('1.0', flds[1])
self.text_intro.delete('1.0', END)
self.text_intro.insert('1.0', flds[2])
self.text_about.delete('1.0', END)
self.text_about.insert('1.0', flds[3] + '(' + flds[4] + ')')
self.text_num.delete('1.0', END)
self.text_num.insert('1.0', 'Total Data Count:' + flds[5] + '\nItems:' + flds[6])
self.text_tools.delete('1.0', END)
line = '\n**************************************************************\nWith Visualization Tools:\n'
self.text_tools.insert('1.0', flds[7] + line + flds[8])
def Forward(self):
rs.MoveNext()
if not rs.EOF:
self.WriteData(rs, flds)
else:
rs.MovePrevious()
#conn.Close()
def Backward(self):
rs.MovePrevious()
if not rs.BOF:
self.WriteData(rs, flds)
else:
rs.MoveNext()
dr = DataReader(root)
root.title("Bacterial & Virus Databases")
root.geometry('800x600+100+100')
root.mainloop()
rs.Close() python真的蛮好用的,以后就用它做数据分析了,以后我要把源码都贴出来,大家不要告密哦。
相关资源:Tkinter编程实例+源码