Question

Program using Python.

This program will draw a user-defined grid on the screen. When run the program will prompt the user for a positive (not 0) number to use for the grid spacing. Make sure to provide a reasonable default, and reasonable max value.

Demo1 Demo1 Grid Grid Grid Enter grid spacing Cance 00:03 1I 00:03

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#!/usr/bin/python

import Tkinter
import turtle


def draw(grid_spacing):
    grid_spacing = int(grid_spacing)  # User defined grid spacing
    grid_window = turtle.Screen()  # Turtle screen
    grid_window.title('Grid')
    # Getting screen width and height
    windowWidth = grid_window.window_width()
    windowHeight = grid_window.window_height()
    if not grid_spacing > 0 and windowHeight / grid_spacing < 2:  # Validating
        grid_spacing = 20
    # Setting coordinates for window
    turtle.setworldcoordinates(-1, -1, windowWidth, windowHeight)
    grid_turtle = turtle.Turtle()  # turtle
    height_of_grid = 0
    width_of_grid = 0
    # Calculating total width of the grids
    for i in range(0, windowWidth - grid_spacing, grid_spacing):
        width_of_grid += grid_spacing
    # Drawing lines horizontally
    for i in range(0, windowHeight - grid_spacing, grid_spacing * 2):
        """In each iteration, the turtle draws a line from left to right
            and then draws the next line from right to left and stops
            at the position to draw the third line from left to right
            again"""
        grid_turtle.forward(width_of_grid)  # Drawing from left to right
        grid_turtle.left(90)  # Turning left
        grid_turtle.forward(grid_spacing)  # Moving spacing distance upwards
        grid_turtle.left(90)  # Again turning left
        grid_turtle.forward(width_of_grid)  # Drawing 2nd line from right to left
        height_of_grid += grid_spacing  # Calculating grid's total height
        """Checking so that turtle stops at the boundary and don't move
            upwards crossing the boundary."""
        if grid_turtle.distance(0, windowHeight) > grid_spacing:
            # Drawing vertical line of spacing length after second line
            grid_turtle.right(90)
            grid_turtle.forward(grid_spacing)
            grid_turtle.right(90)
            height_of_grid += grid_spacing
    # Drawing vertical lines
    grid_turtle.left(90)
    for i in range(0, width_of_grid + grid_spacing, grid_spacing * 2):
        """In each iteration, turtle draws a line from top to bottom
        and then moves it's left to maintain grid distance and then draws
        the next line from bottom to top and then  moves it's right
        again to maintain grid distance for next line."""
        grid_turtle.forward(height_of_grid)  # Moving downwards
        # Checking so that turtle does not cross boundary from the bottom portion
        if grid_turtle.distance(width_of_grid, 0) >= grid_spacing - 1:
            grid_turtle.left(90)
            grid_turtle.forward(grid_spacing)
            grid_turtle.left(90)
            grid_turtle.forward(height_of_grid)
            # Checking do that turtle does not cross boundary from the top line
            if grid_turtle.distance(width_of_grid, height_of_grid) > 1:
                grid_turtle.right(90)
                grid_turtle.forward(grid_spacing)
                grid_turtle.right(90)
    turtle.mainloop()


def main():
    main_window = Tkinter.Tk()  # Creating main window
    main_window.title("Grid")
    user_prompt = Tkinter.Label(main_window, text='Enter grid spacing:')  # Creating label
    user_prompt.pack()  # Placing on main_window
    text_field = Tkinter.Entry(main_window, text="")  # Creating text field
    text_field.pack()
    # Creating buttons
    # command binds a button to a function which will get invoked on click
    buttonOk = Tkinter.Button(main_window, text='OK', command=lambda: draw(text_field.get()))
    buttonOk.pack(side='left', fill='both', expand=True)
    buttonCancel = Tkinter.Button(main_window, text='Cancel', command=lambda: main_window.quit())
    buttonCancel.pack(side='right', fill='both', expand=True)
    main_window.mainloop()  # Running the application


if __name__ == '__main__':
    main()

Output:

Add a comment
Know the answer?
Add Answer to:
Program using Python. This program will draw a user-defined grid on the screen. When run the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program in Python that will allow the user to enter a temperature in Fahrenheit...

    Create a program in Python that will allow the user to enter a temperature in Fahrenheit which will then be converted to degrees Celsius. The program will keep asking the user for a Fahrenheit temperature until the user enters Q to quit. After each conversion the program needs to print out the degrees Celsius. The input prompts for this problem need to look like the following: Degrees Fahrenheit: Continue: For these input prompts watch the capitalization and spacing. There are...

  • Write python program using IDLE Write a full program that asks the user to enter his/her...

    Write python program using IDLE Write a full program that asks the user to enter his/her name then repeatedly ask to enter the temperature in Fahrenheit and convert it to Celsius, the program should then prompt the user if he/she wants to continue or exit the program. The formula for the conversion is: °C = (°F - 32) x 5/9 The program should use a function for the conversion. An Example of a sample run should appear on the screen...

  • Write a C++ program that repeatedly collects positive integers from the user, stopping when the user...

    Write a C++ program that repeatedly collects positive integers from the user, stopping when the user enters a negative number or zero. After that, output the largest positive number entered. A sample run should appear on the screen like the text below. Enter a number: 3 Enter a number: 10 Enter a number: 2 Enter a number: -213 Output: The largest positive number you entered was 10.

  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • Using PYTHON create a program to allow the user to enter a phrase or read a...

    Using PYTHON create a program to allow the user to enter a phrase or read a file. Make your program menu-driven to allow the user to select a option to count the total words in the phrase or file. In addition, provide a menu-selection to count the number of words start with a vowel and a selection to count the number of words that start with a consonant. Include exception handlers for file not found, divide by zero, and index...

  • Question 1 The code used to output messages to the screen begins with run # print...

    Question 1 The code used to output messages to the screen begins with run # print output Flag this Question Question 2 The code used to begin a comment in the Python program source code is # Hello * Comment Flag this Question Question 3 A variable name is like a(n) input typed on a keyboard address in computer memory where values can be stored output to a computer screen value assigned to an address in computer memory Flag this...

  • 1) Write a Python program that prompts the user to enter the current month name and...

    1) Write a Python program that prompts the user to enter the current month name and prints the season for that month. Hint: If the user enters March, the output should be "Spring"; if the user enters June, the output should be "Summer". 2 )Write a Python program using the recursive/loop structure to print out an equilateral triangle below (double spacing and one space between any two adjacent asterisks in the same row).          *      * * * *...

  • write in python idle Write full program that asks the user to enter his/her name then...

    write in python idle Write full program that asks the user to enter his/her name then repeatedly ask to enter the temperature in Fahrenheit and convert it to Celsius, the program should then prompt the user if he/she wants to continue or exit the program. The formula for conversion is: °C = (°F - 32) x 5/9 The program should use a function for the conversion. An Example of a sample run should appear on the screen like the text...

  • This will be done using Python in Linux Mint. The program should include a comment block...

    This will be done using Python in Linux Mint. The program should include a comment block at the top with your name, the program number, and the name of the course. (Make sure to have the file that does have the extension .py) How to run the file in Linux Mint?  Open a command prompt (e.g. terminal)  Make sure you are in the directory where your file is saved (e.g. type "cd ~/Desktop" if your file is on...

  • USING PYTHON *Write a program, in which the user enters two numbers, then divides the first...

    USING PYTHON *Write a program, in which the user enters two numbers, then divides the first number by the second, and prints the result to two decimal places. *Make sure that that the inputs are floats (with exception handling using the ValueError). *Make sure that there is no divide by zero error (with exception handling). *Then have a plain exception: That handles any other error. -Thank you in advance for your assistance-

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT