def AddMenu1(): print('''\033[40m ----------------------------------------------------- AddMenu1 0:退出 1:对《接种人员》表进行添加 2:对《接种信息》表进行添加 3:对《疫苗》表进行添加 4:对《生产企业》表进行添加 ----------------------------------------------------- ''')
时间: 2023-08-15 08:22:37 浏览: 41
这段代码定义了一个AddMenu1()函数,该函数用来打印一个菜单,提供用户进行添加操作的选择。
在打印菜单时,使用了一些特殊的字符序列,比如"\033[40m",这是ANSI控制字符,用来控制终端输出的颜色和样式。在这个例子中,"\033[40m"表示设置输出的背景色为黑色。如果你在终端中运行这个代码时,会看到菜单的背景色变成了黑色。
另外注意到这段代码中使用了三个引号来定义一个多行字符串,这也是Python中的一种常用语法。
相关问题
在PYQT中显示检测标签(0-9)进行打印输出文件,VS2022进行读取文件展示在WIN界面上
以下是一个简单的示例程序,展示了如何在PYQT中显示和打印检测标签,并将输出文件保存到本地。然后,使用VS2022读取保存的文件,并在WIN界面上展示文件内容。
在PYQT中显示和打印检测标签的代码如下:
```python
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtPrintSupport import *
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建菜单栏
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu("File")
# 添加"Print"菜单项
print_action = QAction("Print", self)
print_action.triggered.connect(self.printer)
file_menu.addAction(print_action)
# 创建并设置中心窗口
self.widget = QWidget()
self.setCentralWidget(self.widget)
# 创建布局
layout = QVBoxLayout()
self.widget.setLayout(layout)
# 添加检测标签
self.labels = []
for i in range(10):
label = QLabel(str(i))
self.labels.append(label)
layout.addWidget(label)
def printer(self):
# 打印输出文件
dialog = QFileDialog(self)
dialog.setDefaultSuffix("txt")
file_path, _ = dialog.getSaveFileName(self, "Save File", "", "Text Files (*.txt)")
if file_path:
with open(file_path, "w") as f:
for i in range(10):
f.write(str(i) + "\n")
printer = QPrinter()
printer.setOutputFormat(QPrinter.NativeFormat)
printer.setPaperSize(QPrinter.A4)
printer.setFullPage(True)
painter = QPainter()
painter.begin(printer)
for label in self.labels:
label.render(painter)
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
保存输出文件后,在VS2022中读取并展示文件内容的代码如下:
```c++
#include <fstream>
#include <string>
#include <iostream>
#include <Windows.h>
int main()
{
// 读取文件
std::ifstream file("C:/output.txt");
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
// 在WIN界面上展示文件内容
WCHAR* wtext = new WCHAR[content.size() + 1];
MultiByteToWideChar(CP_ACP, 0, content.c_str(), -1, wtext, content.size() + 1);
wtext[content.size()] = '\0';
MessageBox(NULL, wtext, L"Output File Content", MB_OK);
delete[] wtext;
return 0;
}
```
注意:需要将文件路径`C:/output.txt`替换为实际保存的文件路径。
AttributeError: 'QMenu' object has no attribute 'clicked'
这个错误是因为你正在尝试访问 QMenu 对象的 clicked 属性,但 QMenu 并没有这个属性。QMenu 是一个菜单类,它没有被设计为具有 clicked 属性。
如果你想为一个菜单项添加点击事件,你应该使用 QAction 类。QAction 是一个具有 clicked 信号的动作类,当用户点击菜单项时,可以连接到相应的槽函数。
下面是一个示例代码,演示如何在菜单项被点击时触发事件:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QAction
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
menu = QMenu(self)
action = QAction("Click me", self)
action.triggered.connect(self.on_action_triggered)
menu.addAction(action)
self.menuBar().addMenu(menu)
def on_action_triggered(self):
print("Menu item clicked")
app = QApplication([])
window = MyWindow()
window.show()
app.exec_()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)