Skip to content
Advertisement

How can I make program a bit cleaner?

I have this pretty ugly program (really cluttered) is there any way I could make it cleaner, less cluttered, etc. Thanks! P.S. (If you want to try to run this program prepare to be amazed.)

from tkinter import *
root = Tk()
c = Canvas(root, width = 800, height = 600, bg = "teal")
c.pack()

#ground
c.create_rectangle(0, 550, 800, 600, outline = "Green", fill = "Green")
#house/roof
c.create_rectangle(100, 550, 450, 200, outline = "white", fill = "white")
c.create_line(50, 200, 175, 50, width = 10)
c.create_line(500, 200, 375, 50, width = 10)
c.create_line(165, 50, 385, 50, width = 10)
c.create_line(50, 200, 500, 200, width = 10)
#door+window
c.create_rectangle(150, 550, 260, 300, outline = "Chocolate", fill = "Chocolate")
c.create_rectangle(150, 550, 260, 500, outline = 'black', fill = 'grey')
c.create_rectangle(150, 515, 260, 535, outline = 'black', fill = 'grey')
c.create_rectangle(180, 350, 230, 400, outline = "black", fill = "light blue")
c.create_line(205, 350, 205, 400, width = 5)
c.create_line(180, 375, 230, 375, width = 4)
c.create_rectangle(300, 300, 390, 400, outline = "black", fill = "light blue")
c.create_line(345, 300, 345, 400, width = 5)
c.create_line(300, 350, 390, 350, width = 4)
#stickfigure
c.create_line(600, 550, 640, 490, width = 5)
c.create_line(685, 550, 645, 490, width = 5)
c.create_line(600, 390, 640,  350, width = 5)
c.create_line(685, 390, 645, 350, width = 5)
c.create_line(643, 350, 643, 495, width = 7)
c.create_oval(613, 350, 673, 300, width = 5)
#sun
c.create_oval(800, 100, 700, 0, width = 5, outline = 'yellow', fill = 'yellow')
#trees
c.create_line(70, 550, 70, 480, width = 10, fill = "brown")
c.create_oval(40, 480, 100, 340, fill = 'green')
c.create_line(730, 550, 730, 480, fill = 'brown', width = 10)
c.create_oval(700, 480, 760, 340, fill = 'green')

Advertisement

Answer

First of all, nice work with the drawing!

Here is my suggestion:

# Importing only what is needed
from tkinter import Tk, Canvas


def get_canvas():
    root = Tk()
    canvas = Canvas(root, width=800, height=600, bg="teal")
    canvas.pack()
    return canvas


# Make funcions to re-use the code
def draw_ground(canvas):
    # Using canvas as an parameter so you can re-use the ground on other drawings
    canvas.create_rectangle(0, 550, 800, 600, outline="Green", fill="Green")


def draw_house_roof(canvas):
    canvas.create_rectangle(100, 550, 450, 200, outline="white", fill="white")
    canvas.create_line(50, 200, 175, 50, width=10)
    canvas.create_line(500, 200, 375, 50, width=10)
    canvas.create_line(165, 50, 385, 50, width=10)
    canvas.create_line(50, 200, 500, 200, width=10)


def draw_door_window(canvas):
    canvas.create_rectangle(150, 550, 260, 300, outline="Chocolate", fill="Chocolate")
    canvas.create_rectangle(150, 550, 260, 500, outline="black", fill="grey")
    canvas.create_rectangle(150, 515, 260, 535, outline="black", fill="grey")
    canvas.create_rectangle(180, 350, 230, 400, outline="black", fill="light blue")
    canvas.create_line(205, 350, 205, 400, width=5)
    canvas.create_line(180, 375, 230, 375, width=4)
    canvas.create_rectangle(300, 300, 390, 400, outline="black", fill="light blue")
    canvas.create_line(345, 300, 345, 400, width=5)
    canvas.create_line(300, 350, 390, 350, width=4)


def draw_stickfigure(canvas):
    canvas.create_line(600, 550, 640, 490, width=5)
    canvas.create_line(685, 550, 645, 490, width=5)
    canvas.create_line(600, 390, 640, 350, width=5)
    canvas.create_line(685, 390, 645, 350, width=5)
    canvas.create_line(643, 350, 643, 495, width=7)
    canvas.create_oval(613, 350, 673, 300, width=5)


def draw_sun(canvas):
    canvas.create_oval(800, 100, 700, 0, width=5, outline="yellow", fill="yellow")


def draw_trees(canvas):
    canvas.create_line(70, 550, 70, 480, width=10, fill="brown")
    canvas.create_oval(40, 480, 100, 340, fill="green")
    canvas.create_line(730, 550, 730, 480, fill="brown", width=10)
    canvas.create_oval(700, 480, 760, 340, fill="green")


# Doing this enables you to import these functions in other files
# https://stackoverflow.com/questions/419163/what-does-if-name-main-do
if __name__ == "__main__":
    canvas = get_canvas()
    draw_ground(canvas)
    draw_house_roof(canvas)
    draw_door_window(canvas)
    draw_stickfigure(canvas)
    draw_sun(canvas)
    draw_trees(canvas)
    # Using input to keep the window open.
    input("Press any key to close the windows:")

Things for you to try next:

  • Making the elements size relative to the canvas size
  • Formatting the code with black
  • Use Docstrings and type hinting to improve readability
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement