制作登录对话框如图,输入用户名和密码点击“登录”后如果成功,则关闭登录窗口,打开另一窗口,在该窗口中有一个主菜单,包含“画图”和“学诗词”两个菜单项。“画图”菜单项有一个下拉菜单,包含“扇形”,“三角形”和“矩形”三个菜单项,点击菜单项可在窗口中画出相应图形。“学诗词”菜单项有一个下拉菜单,包含“唐诗”,“宋词”两个菜单项,点击菜单项可在窗口中画出一首唐诗或宋词。
时间: 2023-09-11 18:06:53 浏览: 149
C# Winform中实现主窗口打开登录窗口关闭的方法
好的,我可以为您提供一个使用 Python 和 PyQt5 实现的简单样例程序,满足您的需求。以下是程序代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QMainWindow, QAction, QMenu, QMenuBar, QStatusBar
from PyQt5.QtGui import QPainter, QColor, QPen, QFont
from PyQt5.QtCore import Qt
class LoginDialog(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('登录')
self.setGeometry(300, 300, 300, 200)
self.username_label = QLabel('用户名:', self)
self.username_label.move(50, 50)
self.username_edit = QLineEdit(self)
self.username_edit.move(110, 50)
self.password_label = QLabel('密码:', self)
self.password_label.move(50, 80)
self.password_edit = QLineEdit(self)
self.password_edit.setEchoMode(QLineEdit.Password)
self.password_edit.move(110, 80)
self.login_button = QPushButton('登录', self)
self.login_button.move(110, 120)
self.login_button.clicked.connect(self.login)
self.show()
def login(self):
username = self.username_edit.text()
password = self.password_edit.text()
if username == 'admin' and password == '123456':
self.close()
self.main_window = MainWindow()
self.main_window.show()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('主窗口')
self.setGeometry(300, 300, 400, 300)
self.menu_bar = QMenuBar(self)
self.setMenuBar(self.menu_bar)
self.file_menu = QMenu('文件', self.menu_bar)
self.menu_bar.addMenu(self.file_menu)
self.draw_menu = QMenu('画图', self.menu_bar)
self.menu_bar.addMenu(self.draw_menu)
self.poem_menu = QMenu('学诗词', self.menu_bar)
self.menu_bar.addMenu(self.poem_menu)
self.exit_action = QAction('退出', self)
self.exit_action.triggered.connect(self.close)
self.file_menu.addAction(self.exit_action)
self.sector_action = QAction('扇形', self)
self.sector_action.triggered.connect(self.draw_sector)
self.draw_menu.addAction(self.sector_action)
self.triangle_action = QAction('三角形', self)
self.triangle_action.triggered.connect(self.draw_triangle)
self.draw_menu.addAction(self.triangle_action)
self.rectangle_action = QAction('矩形', self)
self.rectangle_action.triggered.connect(self.draw_rectangle)
self.draw_menu.addAction(self.rectangle_action)
self.tang_poem_action = QAction('唐诗', self)
self.tang_poem_action.triggered.connect(self.show_tang_poem)
self.poem_menu.addAction(self.tang_poem_action)
self.song_poem_action = QAction('宋词', self)
self.song_poem_action.triggered.connect(self.show_song_poem)
self.poem_menu.addAction(self.song_poem_action)
self.status_bar = QStatusBar(self)
self.setStatusBar(self.status_bar)
def draw_sector(self):
self.status_bar.showMessage('正在绘制扇形')
self.painter = QPainter(self)
self.painter.setPen(QPen(Qt.red, 5))
self.painter.drawPie(100, 100, 100, 100, 0, 60)
self.painter.end()
def draw_triangle(self):
self.status_bar.showMessage('正在绘制三角形')
self.painter = QPainter(self)
self.painter.setPen(QPen(Qt.green, 5))
self.painter.drawLine(100, 100, 200, 100)
self.painter.drawLine(200, 100, 150, 150)
self.painter.drawLine(150, 150, 100, 100)
self.painter.end()
def draw_rectangle(self):
self.status_bar.showMessage('正在绘制矩形')
self.painter = QPainter(self)
self.painter.setPen(QPen(Qt.blue, 5))
self.painter.drawRect(100, 100, 100, 50)
self.painter.end()
def show_tang_poem(self):
self.status_bar.showMessage('正在显示唐诗')
self.poem = QLabel(self)
self.poem.setGeometry(100, 100, 200, 200)
self.poem.setText('《静夜思》\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。')
self.poem.setFont(QFont('SimSun', 16))
def show_song_poem(self):
self.status_bar.showMessage('正在显示宋词')
self.poem = QLabel(self)
self.poem.setGeometry(100, 100, 200, 200)
self.poem.setText('《青玉案·元夕》\n东风夜放花千树,\n更吹落、星如雨。\n宝马雕车香满路,\n凤凰涅槃声断续。')
self.poem.setFont(QFont('SimSun', 16))
if __name__ == '__main__':
app = QApplication(sys.argv)
login_dialog = LoginDialog()
sys.exit(app.exec_())
```
通过运行上面的代码,您可以看到一个登录对话框,输入正确的用户名和密码后,可以进入主窗口,其中有“画图”和“学诗词”两个菜单项,可以点击菜单项来绘制图形或显示诗词。
阅读全文