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.)
JavaScript
x
38
38
1
from tkinter import *
2
root = Tk()
3
c = Canvas(root, width = 800, height = 600, bg = "teal")
4
c.pack()
5
6
#ground
7
c.create_rectangle(0, 550, 800, 600, outline = "Green", fill = "Green")
8
#house/roof
9
c.create_rectangle(100, 550, 450, 200, outline = "white", fill = "white")
10
c.create_line(50, 200, 175, 50, width = 10)
11
c.create_line(500, 200, 375, 50, width = 10)
12
c.create_line(165, 50, 385, 50, width = 10)
13
c.create_line(50, 200, 500, 200, width = 10)
14
#door+window
15
c.create_rectangle(150, 550, 260, 300, outline = "Chocolate", fill = "Chocolate")
16
c.create_rectangle(150, 550, 260, 500, outline = 'black', fill = 'grey')
17
c.create_rectangle(150, 515, 260, 535, outline = 'black', fill = 'grey')
18
c.create_rectangle(180, 350, 230, 400, outline = "black", fill = "light blue")
19
c.create_line(205, 350, 205, 400, width = 5)
20
c.create_line(180, 375, 230, 375, width = 4)
21
c.create_rectangle(300, 300, 390, 400, outline = "black", fill = "light blue")
22
c.create_line(345, 300, 345, 400, width = 5)
23
c.create_line(300, 350, 390, 350, width = 4)
24
#stickfigure
25
c.create_line(600, 550, 640, 490, width = 5)
26
c.create_line(685, 550, 645, 490, width = 5)
27
c.create_line(600, 390, 640, 350, width = 5)
28
c.create_line(685, 390, 645, 350, width = 5)
29
c.create_line(643, 350, 643, 495, width = 7)
30
c.create_oval(613, 350, 673, 300, width = 5)
31
#sun
32
c.create_oval(800, 100, 700, 0, width = 5, outline = 'yellow', fill = 'yellow')
33
#trees
34
c.create_line(70, 550, 70, 480, width = 10, fill = "brown")
35
c.create_oval(40, 480, 100, 340, fill = 'green')
36
c.create_line(730, 550, 730, 480, fill = 'brown', width = 10)
37
c.create_oval(700, 480, 760, 340, fill = 'green')
38
Advertisement
Answer
First of all, nice work with the drawing!
Here is my suggestion:
JavaScript
1
71
71
1
# Importing only what is needed
2
from tkinter import Tk, Canvas
3
4
5
def get_canvas():
6
root = Tk()
7
canvas = Canvas(root, width=800, height=600, bg="teal")
8
canvas.pack()
9
return canvas
10
11
12
# Make funcions to re-use the code
13
def draw_ground(canvas):
14
# Using canvas as an parameter so you can re-use the ground on other drawings
15
canvas.create_rectangle(0, 550, 800, 600, outline="Green", fill="Green")
16
17
18
def draw_house_roof(canvas):
19
canvas.create_rectangle(100, 550, 450, 200, outline="white", fill="white")
20
canvas.create_line(50, 200, 175, 50, width=10)
21
canvas.create_line(500, 200, 375, 50, width=10)
22
canvas.create_line(165, 50, 385, 50, width=10)
23
canvas.create_line(50, 200, 500, 200, width=10)
24
25
26
def draw_door_window(canvas):
27
canvas.create_rectangle(150, 550, 260, 300, outline="Chocolate", fill="Chocolate")
28
canvas.create_rectangle(150, 550, 260, 500, outline="black", fill="grey")
29
canvas.create_rectangle(150, 515, 260, 535, outline="black", fill="grey")
30
canvas.create_rectangle(180, 350, 230, 400, outline="black", fill="light blue")
31
canvas.create_line(205, 350, 205, 400, width=5)
32
canvas.create_line(180, 375, 230, 375, width=4)
33
canvas.create_rectangle(300, 300, 390, 400, outline="black", fill="light blue")
34
canvas.create_line(345, 300, 345, 400, width=5)
35
canvas.create_line(300, 350, 390, 350, width=4)
36
37
38
def draw_stickfigure(canvas):
39
canvas.create_line(600, 550, 640, 490, width=5)
40
canvas.create_line(685, 550, 645, 490, width=5)
41
canvas.create_line(600, 390, 640, 350, width=5)
42
canvas.create_line(685, 390, 645, 350, width=5)
43
canvas.create_line(643, 350, 643, 495, width=7)
44
canvas.create_oval(613, 350, 673, 300, width=5)
45
46
47
def draw_sun(canvas):
48
canvas.create_oval(800, 100, 700, 0, width=5, outline="yellow", fill="yellow")
49
50
51
def draw_trees(canvas):
52
canvas.create_line(70, 550, 70, 480, width=10, fill="brown")
53
canvas.create_oval(40, 480, 100, 340, fill="green")
54
canvas.create_line(730, 550, 730, 480, fill="brown", width=10)
55
canvas.create_oval(700, 480, 760, 340, fill="green")
56
57
58
# Doing this enables you to import these functions in other files
59
# https://stackoverflow.com/questions/419163/what-does-if-name-main-do
60
if __name__ == "__main__":
61
canvas = get_canvas()
62
draw_ground(canvas)
63
draw_house_roof(canvas)
64
draw_door_window(canvas)
65
draw_stickfigure(canvas)
66
draw_sun(canvas)
67
draw_trees(canvas)
68
# Using input to keep the window open.
69
input("Press any key to close the windows:")
70
71
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