Tkinter(Python)中弧的选项

2024-09-26 22:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如:

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

“p”是“参数”。在

我不知道p1-p4做什么。在

如何创建一个弧,并将其放置在画布的左下角,以画布的宽度/高度为半径?在

我需要知道这一点,因为我必须为蒙特卡罗模拟编程一个图形用户界面来近似π。在


Tags: 参数画布create数字startextentp2p3
2条回答

坐标表示矩形的对角,该矩形将包围定义圆弧的椭圆。换言之,弧只出现在由该矩形限定的区域内。在

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

Items of type arc appear on the display as arc-shaped regions. An arc is a section of an oval delimited by two angles (specified by the -start and -extent options) and displayed in one of several ways (specified by the -style option).

Arcs are created with widget commands of the following form:

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

The arguments x1, y1, x2, and y2 or coordList give the coordinates of two diagonally opposite corners of a rectangular region enclosing the oval that defines the arc. After the coordinates there may be any number of option-value pairs, each of which sets one of the configuration options for the item. These same option-value pairs may be used in itemconfigure widget commands to change the item's configuration. An arc item becomes the current item when the mouse pointer is over any part that is painted or (when fully transparent) that would be painted if both the -fill and -outline options were non-empty.

当然可以。在

像这样:

# -*- 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))

相关问题 更多 >

    热门问题