Tkinter中的弧选项(Python)

问题描述 投票:0回答:2

.create_arc()的4个初始数量是多少?

如:

myArc = myCanvas.create_arc(p1, p2, p3, p4, start=p5, extent=p6)

'p'是'参数'。

我不知道p1-p4做什么。

如何创建圆弧并将其放置在画布的左下角,并使用画布的宽度/高度半径?

我需要知道这一点,因为我必须为蒙特卡罗模拟编程GUI以近似Pi。

python user-interface canvas tkinter automatic-ref-counting
2个回答
0
投票

坐标表示矩形的相对角,它将包围定义弧的椭圆。换句话说,弧将仅出现在由该矩形限定的区域内。

从官方的tcl / tk文档(tkinter所基于的技术)(参见http://tcl.tk/man/tcl8.5/TkCmd/canvas.htm#M111):

弧形项目在显示器上显示为弧形区域。圆弧是由两个角度(由-start和-extent选项指定)分隔的椭圆的一部分,并以多种方式之一显示(由-style选项指定)。

使用以下格式的窗口小部件命令创建弧:

pathName create arc x1 y1 x2 y2 ?option value option value ...?
pathName create arc coordList ?option value option value ...?

参数x1,y1,x2和y2或coordList给出了包围定义弧的椭圆的矩形区域的两个对角相对的角的坐标。在坐标之后,可以有任意数量的选项 - 值对,每个选项 - 值对设置项目的配置选项之一。可以在itemconfigure小部件命令中使用这些相同的选项 - 值对来更改项的配置。当鼠标指针位于任何绘制的部分上时,或者当完全透明时,如果-fill和-outline选项都非空,则弧项目将成为当前项目。


0
投票

当然可以。

像这样 :

# -*- coding: utf-8 -*-

from Tkinter import *
from random import randint
root = Tk()
can = Canvas(root,width=400.5,height=400,bg="#CCCCCC",highlightbackground="#006699")
can.place(x=10,y=10)

"""
Sec = SECTOR
--------------------
|Sec-1       Sec-2 | 
|                  |
|                  |
|                  |
|                  |
|Sec-4       Sec-3 |
--------------------
"""
deviation = 2 #NEED SHOW ALL ARCs IN CANVAS(FULL)
size = 5  #MEAN WIDTH(BORDER THICKNES)
def setARC(radius,sector,deviation,size):
    w,h = can.config("width")[-1],can.config("height")[-1]
    startAngle = 0
    position = list()
    if sector == 1 :
        positions = 0+deviation,radius+size+deviation,radius+size+deviation,0+deviation
        startAngle = 90
    elif sector == 2 :
        positions = int(w)-radius-size-deviation,deviation,int(w)-deviation,radius+deviation+size
    elif sector == 3:
        positions = int(w)-radius-size-deviation,int(h)-deviation,int(w)-deviation,int(h)-radius-deviation-size
        startAngle = 270
    elif sector == 4:
        positions = deviation,int(h)-radius-deviation-size,radius+deviation+size,int(h)-deviation
        startAngle = 180
    can.create_arc(positions,start=startAngle,extent=90,outline="black",width=size,style=ARC,tags="Sector-"+str(sector))
    sizer.set(radius)

def change(e):
    w,h = int(can.config("width")[-1]),int(can.config("height")[-1])
    radius = sizer.get()
    for x in can.find_all():
        wrd = can.gettags(x)[0]
        if wrd.startswith("Sector-") :
            position = list()
            if wrd[-1] == "1":
                position = [0+deviation,radius+size+deviation,radius+size+deviation,0+deviation]
            elif wrd[-1] == "2":
                position = [w-radius-size-deviation,deviation,w-deviation,radius+deviation+size]
            elif wrd[-1] == "3":
                position = [w-radius-size-deviation,h-deviation,w-deviation,h-radius-deviation-size]
            elif wrd[-1] == "4":
                position = [deviation,h-radius-deviation-size,radius+deviation+size,h-deviation]
            can.coords(x, tuple(position))
            Rcolor = (min(w,h)*255)/(radius if radius > 0 else 1)
            outColor = "#%02X%02X%02X"%tuple(randint(0,255) for i in range(3))
            can.itemconfig(x,outline=outColor)


sizer = Scale(root, from_=0, to=min(can.config("width")[-1],can.config("height")[-1]), orient=VERTICAL,command=change)
sizer.place(x=10+int(can.place_info()["x"])+int(can.config("width")[-1]),y=int(can.place_info()["y"]),\
            height=min(int(can.config("width")[-1]),int(can.config("height")[-1])))

sizer.set(200)

for i in range(1,5,1):
    setARC(200,i,2,5)


root.geometry("800x450+50+50")
root.config(bg="#006699")
root.mainloop()

要考虑的是确定项目的外形尺寸。

w,h = int(can.config("width")[-1]),int(can.config("height")[-1])

您还必须向canvas元素添加标记。

can.create_arc(positions,start=startAngle,extent=90,outline="black",width=size,style=ARC,tags="Sector-"+str(sector))

© www.soinside.com 2019 - 2024. All rights reserved.