请使用graphics库
时间: 2023-09-23 18:07:36 浏览: 47
好的,使用Python的graphics库可以创建图形界面并绘制各种图形。下面是一个简单的例子:
```python
from graphics import *
win = GraphWin('My Window', 500, 500)
# 绘制一个圆
center = Point(250, 250)
radius = 100
circle = Circle(center, radius)
circle.setFill('red')
circle.draw(win)
# 绘制一个矩形
p1 = Point(150, 150)
p2 = Point(350, 350)
rect = Rectangle(p1, p2)
rect.setFill('blue')
rect.draw(win)
# 等待用户点击关闭窗口
win.getMouse()
win.close()
```
这个例子创建了一个大小为500x500的窗口,并在其中绘制了一个红色圆和一个蓝色矩形。用户可以通过点击窗口的关闭按钮来关闭窗口。
相关问题
不使用graphics函数库
如果不使用`graphics.h`图形库,可以使用第三方库(如OpenCV)或自己编写代码来处理图像。以下是一个使用标准C语言库函数(`stdio.h`和`stdlib.h`)和Windows API函数(`windows.h`和`gdiplus.h`)来导入和显示图片的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <gdiplus.h>
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
OPENFILENAME ofn;
char file_name[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Image Files (*.bmp;*.jpg;*.png)\0*.bmp;*.jpg;*.png\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = file_name;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "";
if (GetOpenFileName(&ofn) != TRUE) {
printf("Error: Failed to open image file.\n");
return 1;
}
Gdiplus::Bitmap* bitmap = Gdiplus::Bitmap::FromFile(file_name);
if (bitmap == NULL) {
printf("Error: Failed to load image.\n");
return 1;
}
UINT width = bitmap->GetWidth();
UINT height = bitmap->GetHeight();
BYTE* data = (BYTE*)malloc(width * height * 3);
Gdiplus::Rect rect(0, 0, width, height);
Gdiplus::BitmapData bitmapData;
bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead, PixelFormat24bppRGB, &bitmapData);
BYTE* src = (BYTE*)bitmapData.Scan0;
for (UINT y = 0; y < height; y++) {
BYTE* dst = data + y * width * 3;
for (UINT x = 0; x < width; x++) {
dst[2] = *src++;
dst[1] = *src++;
dst[0] = *src++;
dst += 3;
}
}
bitmap->UnlockBits(&bitmapData);
HWND hwnd = GetDesktopWindow();
HDC hdc = GetDC(hwnd);
HDC memdc = CreateCompatibleDC(hdc);
BITMAPINFOHEADER bmih = { 0 };
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biWidth = width;
bmih.biHeight = -height;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB;
bmih.biSizeImage = 0;
BITMAPINFO bminfo = { 0 };
bminfo.bmiHeader = bmih;
HBITMAP hbitmap = CreateDIBitmap(hdc, &bmih, CBM_INIT, data, &bminfo, DIB_RGB_COLORS);
SelectObject(memdc, hbitmap);
RECT rect2;
GetClientRect(hwnd, &rect2);
int x = (rect2.right - width) / 2;
int y = (rect2.bottom - height) / 2;
BitBlt(hdc, x, y, width, height, memdc, 0, 0, SRCCOPY);
free(data);
DeleteObject(hbitmap);
DeleteDC(memdc);
ReleaseDC(hwnd, hdc);
delete bitmap;
GdiplusShutdown(gdiplusToken);
return 0;
}
```
这个示例程序使用了Windows API中的`GetOpenFileName`函数来让用户选择图像文件,使用GDI+库函数(`GdiplusStartup`、`Gdiplus::Bitmap::FromFile`等)来读取图像数据和显示图像,使用Windows API中的`CreateDIBitmap`和`BitBlt`函数来将图像绘制到窗口中。注意,这个程序只在Windows操作系统上运行。
pythongraphics库使用柱状图
以下是使用Python Graphics库绘制柱状图的示例代码:
```python
from graphics import *
# 创建窗口
win = GraphWin("Bar Chart", 400, 400)
# 定义数据
data = [20, 50, 70, 30, 80]
# 定义柱状图的参数
bar_width = 50
x_start = 50
y_start = 350
y_end = 50
# 绘制柱状图
for i in range(len(data)):
x = x_start + i * bar_width
y = y_start - data[i] * 3
rect = Rectangle(Point(x, y_start), Point(x + bar_width, y))
rect.setFill("blue")
rect.draw(win)
# 显示窗口
win.mainloop()
```
这段代码将创建一个名为“Bar Chart”的窗口,并在其中绘制了一个简单的柱状图。数据存储在列表“data”中,每个柱子的宽度为50,柱子之间的间距为10。柱子的高度根据数据动态计算,最高的柱子将达到窗口的顶部。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.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)