導航:首頁 > 編程語言 > python隨機畫一個圓

python隨機畫一個圓

發布時間:2023-07-12 05:49:37

python pyqt怎麼畫圓

這個例子我做了好幾天:

1)官網C++的源碼,改寫成PyQt5版本的代碼,好多細節不會轉化

2)網上的PyQt的例子根本運行不了

填了無數個坑,結合二者,終於能完成了一個關於繪圖的東西。這個過程也掌握了很多新的知識點

【知識點】

1、關於多個點的使用

poitns = [QPoint(10, 80), QPoint(20, 10), QPoint(80, 30), QPoint(90, 70)]

請看:

import sysfrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *class StockDialog(QWidget): def __init__(self, parent=None):
super(StockDialog, self).__init__(parent)
self.setWindowTitle("利用QPainter繪制各種圖形")

mainSplitter = QSplitter(Qt.Horizontal)
mainSplitter.setOpaqueResize(True)

frame = QFrame(mainSplitter)
mainLayout = QGridLayout(frame) #mainLayout.setMargin(10)
mainLayout.setSpacing(6)

label1=QLabel("形狀:")
label2=QLabel("畫筆線寬:")
label3=QLabel("畫筆顏色:")
label4=QLabel("畫筆風格:")
label5=QLabel("畫筆頂端:")
label6=QLabel("畫筆連接點:")
label7=QLabel("畫刷風格:")
label8=QLabel("畫刷顏色:")

self.shapeComboBox = QComboBox()
self.shapeComboBox.addItem("Line", "Line")
self.shapeComboBox.addItem("Rectangle", "Rectangle")
self.shapeComboBox.addItem('Rounded Rectangle','Rounded Rectangle')
self.shapeComboBox.addItem('Ellipse','Ellipse')
self.shapeComboBox.addItem('Pie','Pie')
self.shapeComboBox.addItem('Chord','Chord')
self.shapeComboBox.addItem('Path','Path')
self.shapeComboBox.addItem('Polygon','Polygon')
self.shapeComboBox.addItem('Polyline','Polyline')
self.shapeComboBox.addItem('Arc','Arc')
self.shapeComboBox.addItem('Points','Points')
self.shapeComboBox.addItem('Text','Text')
self.shapeComboBox.addItem('Pixmap','Pixmap')

self.widthSpinBox = QSpinBox()
self.widthSpinBox.setRange(0,20)

self.penColorFrame = QFrame()
self.penColorFrame.setAutoFillBackground(True)
self.penColorFrame.setPalette(QPalette(Qt.blue))
self.penColorPushButton = QPushButton("更改")

self.penStyleComboBox = QComboBox()
self.penStyleComboBox.addItem("Solid",Qt.SolidLine)
self.penStyleComboBox.addItem('Dash', Qt.DashLine)
self.penStyleComboBox.addItem('Dot', Qt.DotLine)
self.penStyleComboBox.addItem('Dash Dot', Qt.DashDotLine)
self.penStyleComboBox.addItem('Dash Dot Dot', Qt.DashDotDotLine)
self.penStyleComboBox.addItem('None', Qt.NoPen)

self.penCapComboBox = QComboBox()
self.penCapComboBox.addItem("Flat",Qt.FlatCap)
self.penCapComboBox.addItem('Square', Qt.SquareCap)
self.penCapComboBox.addItem('Round', Qt.RoundCap)

self.penJoinComboBox = QComboBox()
self.penJoinComboBox.addItem("Miter",Qt.MiterJoin)
self.penJoinComboBox.addItem('Bebel', Qt.BevelJoin)
self.penJoinComboBox.addItem('Round', Qt.RoundJoin)

self.brushStyleComboBox = QComboBox()
self.brushStyleComboBox.addItem("Linear Gradient",Qt.LinearGradientPattern)
self.brushStyleComboBox.addItem('Radial Gradient', Qt.RadialGradientPattern)
self.brushStyleComboBox.addItem('Conical Gradient', Qt.ConicalGradientPattern)
self.brushStyleComboBox.addItem('Texture', Qt.TexturePattern)
self.brushStyleComboBox.addItem('Solid', Qt.SolidPattern)
self.brushStyleComboBox.addItem('Horizontal', Qt.HorPattern)
self.brushStyleComboBox.addItem('Vertical', Qt.VerPattern)
self.brushStyleComboBox.addItem('Cross', Qt.CrossPattern)
self.brushStyleComboBox.addItem('Backward Diagonal', Qt.BDiagPattern)
self.brushStyleComboBox.addItem('Forward Diagonal', Qt.FDiagPattern)
self.brushStyleComboBox.addItem('Diagonal Cross', Qt.DiagCrossPattern)
self.brushStyleComboBox.addItem('Dense 1', Qt.Dense1Pattern)
self.brushStyleComboBox.addItem('Dense 2', Qt.Dense2Pattern)
self.brushStyleComboBox.addItem('Dense 3', Qt.Dense3Pattern)
self.brushStyleComboBox.addItem('Dense 4', Qt.Dense4Pattern)
self.brushStyleComboBox.addItem('Dense 5', Qt.Dense5Pattern)
self.brushStyleComboBox.addItem('Dense 6', Qt.Dense6Pattern)
self.brushStyleComboBox.addItem('Dense 7', Qt.Dense7Pattern)
self.brushStyleComboBox.addItem('None', Qt.NoBrush)

self.brushColorFrame = QFrame()
self.brushColorFrame.setAutoFillBackground(True)
self.brushColorFrame.setPalette(QPalette(Qt.green))
self.brushColorPushButton = QPushButton("更改")

labelCol=0
contentCol=1
#建立布局
mainLayout.addWidget(label1,1,labelCol)
mainLayout.addWidget(self.shapeComboBox,1,contentCol)
mainLayout.addWidget(label2,2,labelCol)
mainLayout.addWidget(self.widthSpinBox,2,contentCol)
mainLayout.addWidget(label3,4,labelCol)
mainLayout.addWidget(self.penColorFrame,4,contentCol)
mainLayout.addWidget(self.penColorPushButton,4,3)
mainLayout.addWidget(label4,6,labelCol)
mainLayout.addWidget(self.penStyleComboBox,6,contentCol)
mainLayout.addWidget(label5,8,labelCol)
mainLayout.addWidget(self.penCapComboBox,8,contentCol)
mainLayout.addWidget(label6,10,labelCol)
mainLayout.addWidget(self.penJoinComboBox,10,contentCol)
mainLayout.addWidget(label7,12,labelCol)
mainLayout.addWidget(self.brushStyleComboBox,12,contentCol)
mainLayout.addWidget(label8,14,labelCol)
mainLayout.addWidget(self.brushColorFrame,14,contentCol)
mainLayout.addWidget(self.brushColorPushButton,14,3)
mainSplitter1 = QSplitter(Qt.Horizontal)
mainSplitter1.setOpaqueResize(True)

stack1 = QStackedWidget()
stack1.setFrameStyle(QFrame.Panel|QFrame.Raised)
self.area = PaintArea()
stack1.addWidget(self.area)
frame1 = QFrame(mainSplitter1)
mainLayout1 = QVBoxLayout(frame1) #mainLayout1.setMargin(10)
mainLayout1.setSpacing(6)
mainLayout1.addWidget(stack1)

layout = QGridLayout(self)
layout.addWidget(mainSplitter1,0,0)
layout.addWidget(mainSplitter,0,1)
self.setLayout(layout)
#信號和槽函數 self.shapeComboBox.activated.connect(self.slotShape)
self.widthSpinBox.valueChanged.connect(self.slotPenWidth)
self.penColorPushButton.clicked.connect(self.slotPenColor)
self.penStyleComboBox.activated.connect(self.slotPenStyle)
self.penCapComboBox.activated.connect(self.slotPenCap)
self.penJoinComboBox.activated.connect(self.slotPenJoin)
self.brushStyleComboBox.activated.connect(self.slotBrush)
self.brushColorPushButton.clicked.connect(self.slotBrushColor)

self.slotShape(self.shapeComboBox.currentIndex())
self.slotPenWidth(self.widthSpinBox.value())
self.slotBrush(self.brushStyleComboBox.currentIndex())

def slotShape(self,value):
shape = self.area.Shape[value]
self.area.setShape(shape)
def slotPenWidth(self,value):
color = self.penColorFrame.palette().color(QPalette.Window)
style = Qt.PenStyle(self.penStyleComboBox.itemData(self.penStyleComboBox.currentIndex(),Qt.UserRole))
cap = Qt.PenCapStyle(self.penCapComboBox.itemData(self.penCapComboBox.currentIndex(),Qt.UserRole))
join = Qt.PenJoinStyle(self.penJoinComboBox.itemData(self.penJoinComboBox.currentIndex(),Qt.UserRole))
self.area.setPen(QPen(color,value,style,cap,join))
def slotPenStyle(self,value):
self.slotPenWidth(value)
def slotPenCap(self,value):
self.slotPenWidth(value)
def slotPenJoin(self,value):
self.slotPenWidth(value)
def slotPenColor(self):
color = QColorDialog.getColor(Qt.blue)
self.penColorFrame.setPalette(QPalette(color))
self.area.setPen(QPen(color))
def slotBrushColor(self):
color = QColorDialog.getColor(Qt.blue)
self.brushColorFrame.setPalette(QPalette(color))
self.slotBrush(self.brushStyleComboBox.currentIndex())
def slotBrush(self,value):
color = self.brushColorFrame.palette().color(QPalette.Window)
style = Qt.BrushStyle(self.brushStyleComboBox.itemData(value,Qt.UserRole))
if(style == Qt.Lin

⑵ Python如何畫個正六邊形,再畫一個最小的外接圓和一個最大內接圓

Python可以使用turtle庫來繪制圖形,以下汪答是繪制正六邊形、最小外接圓和最大內接圓的代碼:

```python
import turtle
import math

# 創建畫布
canvas = turtle.Screen()

# 創建畫筆
pen = turtle.Turtle()

# 畫正六邊形
for i in range(6):
pen.forward(100)
pen.right(60)

# 計算正六邊形的外接圓半徑
r1 = 100 / math.sin(math.radians(30))

# 將畫筆移動到圓心
pen.penup()
pen.goto(0, 0)
pen.pendown()

# 畫最小外接圓
pen.circle(r1)

# 計算正六邊形的內接圓半徑
r2 = 100 * math.sqrt(3) / 3

# 畫最大內接圓
pen.penup()
pen.goto(0, -r2)
pen.pendown()
pen.circle(r2)

# 隱藏畫筆
pen.hideturtle()

# 關閉畫布
canvas.exitonclick()
```

代碼解析:

首先創建畫布和畫筆,然後使用循環畫正六邊形。接著根據正六邊形的外接圓半徑公式 $R = \frac{a}{\sin{(\frac{\pi}{n})}}$,計算出正六邊形的外接圓半徑 r1。將畫筆移動到圓心,畫最小外接圓。再根據正六邊形的內切圓困仔慧半徑公式 $r = \frac{a\sqrt{3}}{3}$,計算出正六邊形的內戚槐接圓半徑 r2。最後將畫筆移動到內接圓底部,畫最大內切圓。最後隱藏畫筆並關閉畫布。

運行以上代碼就可以得到如下圖所示的正六邊形、最小外接圓和最大內接圓:

![正六邊形、最小外接圓和最大內接圓](https://i.imgur.com/u6UWzBC.png)

⑶ python 繪制一個圓,當單擊窗口的任意位置時圓移動到單擊的位置,如何編寫代碼

#-*-coding:UTF-8-*-


importpygame,sys


frompygame.localsimport*


white=255,255,255

blue=0,0,200

pygame.init()

screen=pygame.display.set_mode((600,800))

myfont=pygame.font.Font(None,20)

textImage=myfont.render("hellogame",True,white)

position=200,200

print(position)

whileTrue:

foreventinpygame.event.get():

ifevent.typein(QUIT,KEYDOWN):

sys.exit()

elifevent.type==MOUSEBUTTONDOWN:

position=event.pos


screen.fill(blue)

screen.blit(textImage,(100,100))

#position=200,200

radius=100

width=10

pygame.draw.circle(screen,white,position,radius,width)

pygame.display.update()

用pygame創建界面並監控滑鼠按下事件,獲得按下位置,畫圓

代碼注意縮進如下圖:

⑷ python turtle畫4個同心圓方法

importturtle
#drawfirstcircle
turtle.penup()
turtle.goto(0,-200)
turtle.pendown()
turtle.circle(200)
#drawsecondcircle
turtle.penup()
turtle.goto(0,-150)
turtle.pendown()
turtle.circle(150)
#drawthirdcircle
turtle.penup()
turtle.goto(0,-100)
turtle.pendown()
turtle.circle(100)
#drawfourthcircle
turtle.penup()
turtle.goto(0,-50)
turtle.pendown()
turtle.circle(50)


畫筆的坐標默認在0,0,就以它為圓心。

因為turtle畫圓的時候是從圓的底部開始畫的,所以需要找到四個圓底部的坐標

比如:

第一個半徑為200的圓,底部為(0,-200)

第二個半徑為150的圓,底部為(0,-150)

第三個半徑為100的圓,底部為(0,-100)

第四個半徑為 50的圓,底部為(0, -50)


畫的時候按下面的步驟:

  1. 抬起畫筆:turtle.penup()

  2. 移動到相應坐標:turtle.goto(坐標)

  3. 放下畫筆:turtle.pendown()

  4. 畫圓:turtle.circle(半徑)


效果如下圖所示:



⑸ 用python畫一個圓

###################################
# coding=utf-8
# !/usr/bin/env python
# __author__ = 'pipi'
# ctime 2014.10.11
# 繪制橢圓和圓形
###################################
from matplotlib.patches import Ellipse, Circle
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

ell1 = Ellipse(xy = (0.0, 0.0), width = 4, height = 8, angle = 30.0, facecolor= 'yellow', alpha=0.3)
cir1 = Circle(xy = (0.0, 0.0), radius=2, alpha=0.5)
ax.add_patch(ell1)
ax.add_patch(cir1)

x, y = 0, 0
ax.plot(x, y, 'ro')

plt.axis('scaled')
# ax.set_xlim(-4, 4)
# ax.set_ylim(-4, 4)
plt.axis('equal') #changes limits of x or y axis so that equal increments of x and y have the same length

plt.show()

你可以試試,謝謝。

⑹ python中turtle畫圓的代碼

import turtle
turtle.circle(80)

⑺ python中點圓畫法

1、我們的電腦上要安裝有Python,我們找到安裝的Python並打開。
2、在打開的Python窗口中執行文件菜單的新建命令,新建一個idle的文件項目。
3、在新建的idle項目中輸入下面代碼:importturtle、turtle.color("blue")turtle.circle(100)繪制出第一個大圓出來。
4、下面執行第一個帶蔽大圓的填充命令,執行下面代碼:importturtle、褲行配turtle.color("blue")、turtle.begin_fill()、turtle.circle(100)、turtle.end_fill()、通過turtle.begin_fill()與turtle.endfill()。
5、下面來畫第二個小圓,需要先跳轉畫筆,並更改畫筆顏色,畫出第胡指二個小圓。跳轉畫筆使用goto命令。
6、最後畫中間的小圓操作和大圓一樣,執行填充命令即可。這樣一個圓中圓的效果就完成了。

⑻ python怎麼用turtle畫圓

turtle.circle ()

Turtle庫是Python語言中一個很流行的繪制圖像的函數庫,想像一個小烏龜,在一個橫軸為x、縱軸為y的坐標系原點,(0,0)位置開始,它根據一組函數指令的控制,在這個平面坐標系中移動,從而在它爬行的路徑上繪制了圖形。

⑼ python 如何畫圓

1.調用包函數繪制圓形Circle和橢圓Ellipse

閱讀全文

與python隨機畫一個圓相關的資料

熱點內容
51單片機歷史 瀏覽:888
怎樣減小pdf文件大小 瀏覽:31
和彩雲zip解壓 瀏覽:723
linux查看線程狀態 瀏覽:457
怎麼起文件夾名稱 瀏覽:47
北京交大編譯原理 瀏覽:980
linux執行java腳本 瀏覽:587
全能掃描加密文檔忘記密碼怎麼辦 瀏覽:73
極品飛車ol安卓版為什麼要關服 瀏覽:273
學生伺服器怎麼選 瀏覽:462
mac系統本身編譯器 瀏覽:149
頭條app如何設置橫屏模式 瀏覽:359
clion怎麼使用終端編譯 瀏覽:766
伺服器地址部署到公網 瀏覽:492
新桑塔納安卓大屏導航怎麼拆 瀏覽:382
程序員送給女友的禮物 瀏覽:432
ftp命令行查看文件數量 瀏覽:506
linux查看設備的命令 瀏覽:837
pythongolang學哪個 瀏覽:351
金蝶加密鎖驅動下載 瀏覽:300