使用graphics绘制资源分配图
时间: 2023-06-28 18:12:59 浏览: 54
以下是使用graphics库绘制资源分配图的示例代码:
```python
from graphics import *
# 创建窗口
win = GraphWin("Resource Allocation Diagram", 600, 400)
# 绘制矩形框
rect = Rectangle(Point(100, 50), Point(500, 350))
rect.draw(win)
# 绘制资源池
pool1 = Rectangle(Point(120, 70), Point(220, 130))
pool1.setFill("gray")
pool1.draw(win)
pool2 = Rectangle(Point(280, 70), Point(380, 130))
pool2.setFill("gray")
pool2.draw(win)
pool3 = Rectangle(Point(440, 70), Point(540, 130))
pool3.setFill("gray")
pool3.draw(win)
# 绘制资源分配情况
task1 = Rectangle(Point(120, 150), Point(220, 210))
task1.setFill("red")
task1.draw(win)
task2 = Rectangle(Point(280, 230), Point(380, 290))
task2.setFill("yellow")
task2.draw(win)
task3 = Rectangle(Point(440, 150), Point(540, 210))
task3.setFill("green")
task3.draw(win)
# 标注名称
pool1_label = Text(Point(170, 100), "Resource Pool 1")
pool2_label = Text(Point(320, 100), "Resource Pool 2")
pool3_label = Text(Point(480, 100), "Resource Pool 3")
task1_label = Text(Point(170, 180), "Task 1")
task2_label = Text(Point(320, 260), "Task 2")
task3_label = Text(Point(480, 180), "Task 3")
pool1_label.draw(win)
pool2_label.draw(win)
pool3_label.draw(win)
task1_label.draw(win)
task2_label.draw(win)
task3_label.draw(win)
# 等待窗口关闭
win.getMouse()
win.close()
```
运行上述代码后,会生成一个窗口,窗口中包含了一个矩形框和若干个矩形表示资源池和任务,并且标注了名称。这个窗口会一直保持打开状态,直到用户点击关闭按钮。可以根据自己的需要修改上述代码,以适应不同的资源分配情况。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)