from PyQt5.QtWidgets import QWidget,QLabel,QLineEdit,QGroupBox,QGridLayout,QVBoxLayout,QPushButton,QMessageBox import pickle,pymssql,os,threading,time #定義數(shù)據(jù)庫(kù)窗口 class db_window(QWidget): def __init__(self): super().__init__() self.db_server = QLineEdit() self.db_port = QLineEdit() self.db_user = QLineEdit() self.db_password = QLineEdit() self.dir_db = {} #初始化數(shù)據(jù)庫(kù)鏈接信息 self.db_test_flag = -1 self.dir_test_db = {} self.initUI() self.initDB() def initUI(self): #初始化頁面 db_server_label = QLabel('數(shù)據(jù)庫(kù)地址:') db_port_label = QLabel('端口:') db_user_label = QLabel('數(shù)據(jù)庫(kù)賬號(hào):') db_password_label = QLabel('數(shù)據(jù)庫(kù)密碼:') self.db_password.setEchoMode(QLineEdit.Password) db_test_button = QPushButton('測(cè)試數(shù)據(jù)庫(kù)鏈接') db_input_button = QPushButton('保存數(shù)據(jù)庫(kù)鏈接') db_test_button.clicked.connect(self.test_db) db_input_button.clicked.connect(self.save_db) db_dbinfo_group = QGroupBox('MSSQL數(shù)據(jù)庫(kù)') db_mssql_grid = QGridLayout() db_mssql_grid.addWidget(db_server_label,0,0) db_mssql_grid.addWidget(db_port_label,0,1) db_mssql_grid.addWidget(db_user_label,0,2) db_mssql_grid.addWidget(db_password_label,0,3) db_mssql_grid.addWidget(db_test_button,0,4) db_mssql_grid.addWidget(self.db_server,1,0) db_mssql_grid.addWidget(self.db_port,1,1) db_mssql_grid.addWidget(self.db_user,1,2) db_mssql_grid.addWidget(self.db_password,1,3) db_mssql_grid.addWidget(db_input_button,1,4) db_dbinfo_group.setLayout(db_mssql_grid) vbox = QVBoxLayout() vbox.addWidget(db_dbinfo_group) self.test_label = QLabel('') vbox.addWidget(self.test_label) vbox.setStretchFactor(db_dbinfo_group,3) vbox.setStretchFactor(self.test_label,7) self.setLayout(vbox) #初始化數(shù)據(jù)庫(kù)鏈接信息 def initDB(self): try: if os.path.exists('.//DATA//DBinfo.pik'): with open('.//DATA//DBinfo.pik','rb') as db_file: self.dir_db = pickle.load(db_file) for line in self.dir_db.keys(): if line == 'server': self.db_server.setText(self.dir_db['server']) elif line == 'port': self.db_port.setText(str(self.dir_db['port'])) elif line == 'user': self.db_user.setText(self.dir_db['user']) elif line == 'password': self.db_password.setText(self.dir_db['password']) except Exception as error: return #數(shù)據(jù)庫(kù)測(cè)試按鈕 def test_db(self): self.dir_test_db.clear() if self.db_server.text().strip() != '': self.dir_test_db['server'] = self.db_server.text().strip() if self.db_port.text().strip() != '': try: port = int(self.db_port.text().strip()) except Exception as error: QMessageBox.warning(self,'端口錯(cuò)誤','
新聞標(biāo)題:pyqt5_站點(diǎn)管理_db_windows
網(wǎng)頁鏈接:http://weahome.cn/article/psoose.html