<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Tkinter 中的繪圖 > 原文: [http://zetcode.com/tkinter/drawing/](http://zetcode.com/tkinter/drawing/) 在 Tkinter 教程的這一部分中,我們將進行一些繪制。 在`Canvas`小部件上完成了 Tkinter 的繪制。 `Canvas`是用于在 Tkinter 中進行圖形處理的高級工具。 它可用于創建圖表,自定義窗口小部件或創建游戲。 ## 直線 線是簡單的幾何圖元。 `create_line()`方法在`Canvas`上創建一個訂單項。 `lines.py` ```py #!/usr/bin/env python3 """ ZetCode Tkinter tutorial The example draws lines on the Canvas. Author: Jan Bodnar Last modified: April 2019 Website: www.zetcode.com """ from tkinter import Tk, Canvas, Frame, BOTH class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("Lines") self.pack(fill=BOTH, expand=1) canvas = Canvas(self) canvas.create_line(15, 25, 200, 25) canvas.create_line(300, 35, 300, 200, dash=(4, 2)) canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85) canvas.pack(fill=BOTH, expand=1) def main(): root = Tk() ex = Example() root.geometry("400x250+300+300") root.mainloop() if __name__ == '__main__': main() ``` 在代碼示例中,我們繪制了簡單的線條。 ```py canvas.create_line(15, 25, 200, 25) ``` `create_line()`方法的參數是直線起點和終點的 x 和 y 坐標。 ```py canvas.create_line(300, 35, 300, 200, dash=(4, 2)) ``` 畫一條垂直線。 `dash`選項指定線條的虛線圖案。 我們有一條線,由 4px 虛線和 2px 間隔的交替部分組成。 ```py canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85) ``` `create_line()`方法可以取多個點。 這條線畫了一個三角形。 ![Lines](https://img.kancloud.cn/63/96/639690c89155e2fa0f26389399b9242d_402x276.jpg) 圖:直線 ## 顏色 顏色是代表紅色,綠色和藍色(RGB)強度值的組合的對象。 `colours.py` ```py #!/usr/bin/env python3 """ ZetCode Tkinter tutorial This program draws three rectangles filled with different colours. Author: Jan Bodnar Last modified: April 2019 Website: www.zetcode.com """ from tkinter import Tk, Canvas, Frame, BOTH class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("Colours") self.pack(fill=BOTH, expand=1) canvas = Canvas(self) canvas.create_rectangle(30, 10, 120, 80, outline="#fb0", fill="#fb0") canvas.create_rectangle(150, 10, 240, 80, outline="#f50", fill="#f50") canvas.create_rectangle(270, 10, 370, 80, outline="#05f", fill="#05f") canvas.pack(fill=BOTH, expand=1) def main(): root = Tk() ex = Example() root.geometry("400x100+300+300") root.mainloop() if __name__ == '__main__': main() ``` 在代碼示例中,我們繪制了三個矩形,并用不同的顏色值填充了它們。 ```py canvas = Canvas(self) ``` 我們創建`Canvas`小部件。 ```py canvas.create_rectangle(30, 10, 120, 80, outline="#fb0", fill="#fb0") ``` `create_rectangle()`在畫布上創建一個矩形項目。 前四個參數是兩個邊界點的 x 和 y 坐標:左上角點和右下角點。 使用`outline`參數,我們可以控制矩形輪廓的顏色。 同樣,`fill`參數為矩形的內部提供顏色。 ![Colours](https://img.kancloud.cn/63/30/63302f184ff6b612d7c28520621cae7e_402x126.jpg) 圖:顏色 ## 形狀 我們可以在`Canvas`上繪制各種形狀。 以下代碼示例將顯示其中的一些。 `shapes.py` ```py #!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this script, we draw basic shapes on the canvas. Author: Jan Bodnar Last modified: April 2019 Website: www.zetcode.com """ from tkinter import Tk, Canvas, Frame, BOTH class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("Shapes") self.pack(fill=BOTH, expand=1) canvas = Canvas(self) canvas.create_oval(10, 10, 80, 80, outline="#f11", fill="#1f1", width=2) canvas.create_oval(110, 10, 210, 80, outline="#f11", fill="#1f1", width=2) canvas.create_rectangle(230, 10, 290, 60, outline="#f11", fill="#1f1", width=2) canvas.create_arc(30, 200, 90, 100, start=0, extent=210, outline="#f11", fill="#1f1", width=2) points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200] canvas.create_polygon(points, outline='#f11', fill='#1f1', width=2) canvas.pack(fill=BOTH, expand=1) def main(): root = Tk() ex = Example() root.geometry("330x220+300+300") root.mainloop() if __name__ == '__main__': main() ``` 我們在窗口上繪制五個不同的形狀:一個圓形,一個橢圓形,一個矩形,一個弧形和一個多邊形。 輪廓以紅色繪制,內部以綠色繪制。 輪廓的寬度為 2 像素。 ```py canvas.create_oval(10, 10, 80, 80, outline="#f11", fill="#1f1", width=2) ``` 此處`create_oval()`方法用于創建圈子項目。 前四個參數是圓的邊界框坐標。 換句話說,它們是在其中繪制圓的框的左上和右下點的 x 和 y 坐標。 ```py canvas.create_rectangle(230, 10, 290, 60, outline="#f11", fill="#1f1", width=2) ``` 我們創建一個矩形項目。 坐標還是要繪制的矩形的邊界框。 ```py canvas.create_arc(30, 200, 90, 100, start=0, extent=210, outline="#f11", fill="#1f1", width=2) ``` 該代碼行創建了一條弧。 圓弧是圓的圓周的一部分。 我們提供邊界框。 `start`參數是圓弧的起始角度。 `extent`是角度大小。 ```py points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200] canvas.create_polygon(points, outline='#f11', fill='#1f1', width=2) ``` 創建一個多邊形。 它是具有多個角的形狀。 要在 Tkinter 中創建多邊形,我們向`create_polygon()`方法提供了多邊形坐標列表。 ![Shapes](https://img.kancloud.cn/93/a4/93a4c4669c1fd192e4e99c0dbef4f4f7_332x245.jpg) 圖:形狀 ## 繪制圖像 在下面的示例中,我們在畫布上繪制一個圖像項。 `draw_image.py` ```py #!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this script, we draw an image on the canvas. Author: Jan Bodnar Last modified: April 2019 Website: www.zetcode.com """ from tkinter import Tk, Canvas, Frame, BOTH, NW from PIL import Image, ImageTk class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("High Tatras") self.pack(fill=BOTH, expand=1) self.img = Image.open("tatras.jpg") self.tatras = ImageTk.PhotoImage(self.img) canvas = Canvas(self, width=self.img.size[0]+20, height=self.img.size[1]+20) canvas.create_image(10, 10, anchor=NW, image=self.tatras) canvas.pack(fill=BOTH, expand=1) def main(): root = Tk() ex = Example() root.mainloop() if __name__ == '__main__': main() ``` 該示例在畫布上顯示圖像。 ```py from PIL import Image, ImageTk ``` 從 PIL(Python 圖像庫)模塊,導入`Image`和`ImageTk`模塊。 ```py self.img = Image.open("tatras.jpg") self.tatras = ImageTk.PhotoImage(self.img) ``` Tkinter 在內部不支持 JPG 圖像。 解決方法是,使用`Image`和`ImageTk`模塊。 ```py canvas = Canvas(self, width=self.img.size[0]+20, height=self.img.size[1]+20) ``` 我們創建`Canvas`小部件。 它考慮了圖像的大小。 它比實際圖像大小寬 20px,高 20px。 ```py canvas.create_image(10, 10, anchor=NW, image=self.tatras) ``` 我們使用`create_image()`方法在畫布上創建一個圖像項。 為了顯示整個圖像,它固定在北部和西部。 `image`參數提供要顯示的照片圖像。 ## 繪制文字 在最后一個示例中,我們將在窗口上繪制文本。 `draw_text.py` ```py #!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this script, we draw text on the window. Author: Jan Bodnar Last modified: April 2019 Website: www.zetcode.com """ from tkinter import Tk, Canvas, Frame, BOTH, W class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("Lyrics") self.pack(fill=BOTH, expand=1) canvas = Canvas(self) canvas.create_text(20, 30, anchor=W, font="Purisa", text="Most relationships seem so transitory") canvas.create_text(20, 60, anchor=W, font="Purisa", text="They're good but not the permanent one") canvas.create_text(20, 130, anchor=W, font="Purisa", text="Who doesn't long for someone to hold") canvas.create_text(20, 160, anchor=W, font="Purisa", text="Who knows how to love without being told") canvas.create_text(20, 190, anchor=W, font="Purisa", text="Somebody tell me why I'm on my own") canvas.create_text(20, 220, anchor=W, font="Purisa", text="If there's a soulmate for everyone") canvas.pack(fill=BOTH, expand=1) def main(): root = Tk() ex = Example() root.geometry("420x250+300+300") root.mainloop() if __name__ == '__main__': main() ``` 我們在窗口上畫一首歌的歌詞。 ```py canvas.create_text(20, 30, anchor=W, font="Purisa", text="Most relationships seem so transitory") ``` 前兩個參數是文本中心點的 x 和 y 坐標。 如果我們將文本項錨定在西方,則文本將從該位置開始。 `font`參數提供文本的字體,`text`參數是要顯示的文本。 ![Drawing text](https://img.kancloud.cn/69/07/6907f3938bd9191048c43fc16bcabbea_422x276.jpg) 圖:繪制文本 在 Tkinter 教程的這一部分中,我們做了一些繪圖。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看