python程序设计
------天气管理系统
本人是在校大二学生,菜鸟一枚,我今天编写的是我大二下学期的python这门课的课程设计,来跟大家学习交流一下。
我这个项目用到了pycharm2019.3.2、mysql_workbench以及PYQT5.
这是需要导入的模块
实现的大致功能:在登陆界面选择是注册新用户还是老用户登陆
如果是注册的话就会进入另一个widget图形界面内,注册好的信息会自动保存在workbench所见的表里;使用用户名和密码登陆后进入主页有添加天气、市区、编辑、删除等,此项目主要用的是sql语句的增删改查操作。
部分代码
ui, _ = loadUiType(‘weather.ui’)
user, _ = loadUiType(‘user.ui’)
LG, _ = loadUiType(‘login.ui’)
登陆界面
class UserAPP(QWidget, user):
def init(self):
QWidget.init(self)
self.setupUi(self)
self.loginButton.clicked.connect(self.handellogin)
self.login_Button.clicked.connect(self.handel_login)
def md5(self, arg):
hash = hashlib.md5(bytes("柊堇桑", encoding="utf-8"))
hash.update(bytes(arg, encoding='utf-8'))
return hash.hexdigest()
def handellogin(self):
conn = get_conn()
cur = conn.cursor()
sql = "select * from user where user_name=%s and user_pwd=%s"
user_name = self.user_name.text()
pwd = self.user_pwd.text()
pwd = self.md5(pwd)
cur.execute(sql, (user_name, pwd))
data = cur.fetchone()
if data:
self.main_app = MainApp()
self.close()
self.main_app.show()
else:
self.error_message.setText("用户名或密码错误,请重新输入!")
def handel_login(self):
self.main_app = LoginAPP()
self.main_app.show()
主界面
class MainApp(QMainWindow, ui):
# 定义构造方法
def init(self):
QMainWindow.init(self)
self.setupUi(self)
self.handle_ui_change()
self.handle_ui_change()
self.handle_buttons()
self.show_condition()
self.show_humidity()
self.show_AQItype()
self.show_wind_power()
self.show_wind_direction()
self.show_town()
self.show_add_condition_comboBox()
self.show_humidity_comboBox()
self.show_wind_power_comboBox()
self.show_AQItype_comboBox()
self.show_wind_direction_comboBox()
self.show_weather()
self.show_add_town_comboBox()
self.show_all_users()
self.show_notice()
# UI变化处理
def handle_ui_change(self):
self.tabWidget.tabBar().setVisible(False)
# 槽的处理
def handle_buttons(self):
self.add_weather_condition_Button.clicked.connect(self.add_condition)
self.add_humidity_Button.clicked.connect(self.add_humidity)
self.add_AQItype_Button.clicked.connect(self.add_AQItype)
self.wind_power_Button.clicked.connect(self.add_wind_power)
self.wind_direction_Button.clicked.connect(self.add_wind_direction)
self.add_town_Button.clicked.connect(self.add_town)
self.delete_weather_Button.clicked.connect(self.delete_weather)
self.editor_weather_change_Button.clicked.connect(self.editor_weather)
self.editor_town_search_button.clicked.connect(self.search_weather)
self.add_weather_Button.clicked.connect(self.add_weather)
self.add_user_Button.clicked.connect(self.add_user)
self.user_login_button.clicked.connect(self.user_login)
self.editor_user_Button.clicked.connect(self.editor_user)
self.weatherbutton.clicked.connect(self.open_weather_tab)
self.databutton.clicked.connect(self.open_data_tab)
self.userbutton.clicked.connect(self.open_user_tab)
self.noticebutton.clicked.connect(self.open_notice_tab)
self.add_notice_Button.clicked.connect(self.add_notice)
def open_notice_tab(self):
self.tabWidget.setCurrentIndex(0)
添加天气状况
def add_condition(self):
conn = get_conn()
cur = conn.cursor()
sql = “insert into weather_condition(weather_condition_name) values(%s)”
weather_condition_name = self.add_weather_condition.text()
cur.execute(sql, (weather_condition_name,))
conn.commit()
close_conn(conn, cur)
self.statusBar().showMessage(‘天气状况添加成功!’)
self.add_weather_condition.setText(’’)
self.show_condition()
def show_condition(self):
conn = get_conn()
cur = conn.cursor()
sql = “select weather_condition_name from weather_condition”
cur.execute(sql)
# 取其中所有的数据
data = cur.fetchall()
if data:
# 如果rowcount是零行
self.weather_condition_table.setRowCount(0)
# 则插入行
self.weather_condition_table.insertRow(0)
# 二维行和列循环
for row, form in enumerate(data):
for column, item in enumerate(form):
self.weather_condition_table.setItem(row, column, QTableWidgetItem(str(item)))
column += 1
row_position = self.weather_condition_table.rowCount()
self.weather_condition_table.insertRow(row_position)
显示在下拉列表内
def show_add_condition_comboBox(self):
conn = get_conn()
cur = conn.cursor()
sql = “select weather_condition_name from weather_condition”
cur.execute(sql)
data = cur.fetchall()
print(data)
if data:
self.add_condition_comboBox.clear()
self.editor_condition_comboBox.clear()
for weather_condition in data:
self.add_condition_comboBox.addItem(weather_condition[0])
self.editor_condition_comboBox.addItem(weather_condition[0])
搜索天气
def search_weather(self):
conn = get_conn()
cur = conn.cursor()
sql = “select * from weathers where weathers_town= %s”
weathers_town = self.editor_town_name_search.text()
cur.execute(sql, (weathers_town,))
data = cur.fetchone()
print(data)
if data:
self.editor_weathers_tempe.setText(data[1])
self.editor_condition_comboBox.setCurrentText(data[2])
self.editor_wind_direction_comboBox.setCurrentText(data[3])
self.editor_humidity_comboBox.setCurrentText(data[4])
self.editor_weathers_aqi.setText(data[5])
self.editor_AQItype_comboBox.setCurrentText(data[6])
self.editor_wind_power_comboBox.setCurrentText(data[7])
self.editor_town_name_search.setText(data[8])
self.editor_weathers_time.setText(data[9])
else:
self.statusBar().showMessage(‘系统并未找到此市区,请核对后输入正确市区名称!!’)
def main():
app = QApplication([])
window = UserAPP()
window.show()
app.exec_()
if name == ‘main’:
main()