Chapter 18: Graphics Under Windows

Hello Windows, Page number: 582

In [ ]:
from tkinter import *


class Example(Frame):
        def __init__(self, parent):
            Frame.__init__(self, parent)

            self.display = Canvas(self, width=700, height=200)
            self.display.pack(side="top", fill="both", expand=True)
            self.display.create_text(10, 10, fill = "blue",text = "Hello Windows", font="Arial 20 italic",
                                          anchor="nw")
            self.display.create_text(10, 50, fill = "blue",text = "Hello Windows", font="TimesNewRoman 30 italic",
                                          anchor="nw")
            self.display.create_text(10, 100, fill = "blue",text = "Hello Windows", font="ComicSansMS 40 italic",
                                          anchor="nw")

if __name__ == "__main__":
    root = Tk()
    root.title("Text")
    Frame = Example(parent=root)
    Frame.pack(side="top", fill="both", expand=True)
    root.mainloop()

Drawing Shapes, Page number: 587

In [ ]:
from tkinter import *


top = Tk()
top.title("Shapes")
C = Canvas(top, height=500, width=500)
rcoor = 10,20,200,100
rect = C.create_rectangle(rcoor,fill="blue")#rectangle
ecoor = 10,280,200,380
ellipse = C.create_oval(ecoor,fill = "blue")#ellipse
picoor = 250,0,350,100
pie = C.create_arc(picoor, start=300, extent=100, fill="blue")#pie
pocoor = 250, 150, 250, 300, 300, 350, 400, 300, 320, 190
polygon = C.create_polygon(pocoor,fill="blue")#polygon
#roundedrectangle
c1= C.create_arc(155,115,195,150,start=320, extent=80, fill="blue",outline="blue")
c2= C.create_arc(155,208,195,243,start=320, extent=80, fill="blue",outline="blue")
c3= C.create_arc(25,118,60,153,start=100, extent=150, fill="blue",outline="blue")
c4= C.create_arc(25,207,60,242,start=100, extent=150, fill="blue",outline="blue")
roundrect = C.create_rectangle(30,120,190,240,fill="blue",outline="blue")
C.pack()
top.mainloop()

Pen Styles, Page number: 590

In [ ]:
from tkinter import *


top = Tk()
top.title("Pen styles")
C = Canvas(top, height=100, width=500)
l1 = C.create_line(0,10,500,10,fill="red",dash=(5)) #dashed line
l2 = C.create_line(0,30,500,30,fill="red",dash=(1)) #dotted line
l3 = C.create_line(0,50,500,50,fill="red",dash=(5,1,1,1)) #dash dot
l4 = C.create_line(0,70,500,70,fill="red",dash=(5,1,1,1,1)) #dash dot dot
l5 = C.create_line(0,90,500,90,fill="red",width=4) #solid line
C.pack()
top.mainloop()

Types of Brushes, Page number: 592

In [ ]:
import sys
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):      

        self.setGeometry(300, 300, 355, 280)
        self.setWindowTitle('Brush Styles')
        self.show()

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawBrushes(qp)
        qp.end()
        
    def drawBrushes(self, qp):
      
        brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 15, 90, 60)

        brush.setStyle(QtCore.Qt.CrossPattern)
        qp.setBrush(brush)
        qp.drawRect(130, 15, 90, 60)

       
        image = QtGui.QImage("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg")
        brush.setTextureImage (image)
        qp.setBrush(brush)
        qp.drawRect(250, 15, 90, 60)

       
              
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Displaying a Bitmap, Page number: 605

In [ ]:
from tkinter import *


top = Tk()
top.title("Pen styles")
C = Canvas(top, height=300, width=500)
filename = PhotoImage(file = "C:/Users/Akshatha M/Desktop/dialog1.gif")
image = C.create_image(50, 50, anchor=NE, image=filename)
C.pack()
top.mainloop()

Animation at Work, Page number: 608

In [ ]:
from visual import *

floor = box(length=4, height=0.5, width=4, color=color.blue)

ball = sphere(pos=(0,4,0), color=color.red)
ball.velocity = vector(0,-1,0)

dt = 0.01
while 1:
    rate(100)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y < 1:
        ball.velocity.y = -ball.velocity.y
    else:
        ball.velocity.y = ball.velocity.y - 9.8*dt