有关根据对话框编号输入更改参数值的问题

时间:2015-12-12 03:58:01

标签: python python-3.x turtle-graphics

我正在尝试创建一个程序,用户可以通过按下将激活该函数来绘制该字母的特定键来绘制程序中的字母。到目前为止,我有“H”,“E”,“L”,“O”,“W”,“R”和“D”。我打算稍后再添加。但是,现在我遇到了一个问题,我希望用户能够通过特定的按键来更改字母高度和高度。宽度来自他们在开头设置的内容,我希望用户能够从出现的对话框中执行此操作,并且\我可以使用函数turtle.numinput()完成此操作到目前为止,我在下面定义的函数中显示了这一点:

from turtle import *


def NewLetterDimensions():
    def NewLetterHeight(letter_height):
        letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
    def NewLetterWidth(letter_width):
        letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
    NewLetterHeight(letter_height)
    NewLetterWidth(letter_width)
    listen()

用户可以根据键“K”调用它,如下所示:

onkey(NewLetterDimensions, "k")

以下是代码的其余部分:

    """ A simple typing program

Press "H" to draw an H
Press "E" to draw an E
Press "L" to draw a L
Press "O" to draw an O
Press "W" to draw a W
Press "R" to draw a R
Press "D" to draw a D
Press "ENTER" to go to the next line
Press "TAB" to make an indent
Press "BACKSPACE" to go backwards an indent
Press the "Up" or "Down" arrow keys to move the turtle up or down the canvas
Press "SPACE" to clear the canvas of your drawings
Press "ESC" to exit the program
Press any number from 1-0 on the key board (1 = 1, 0 = 10) to set the letter thickness
Press any key from F1-F10 to set a random color of the letter
(F1-F8 = colors, F9 = Custom Color, F10 = Original color)

That is all you need to know! Enjoy! """

from turtle import *
from math import *
from tkinter import *
from tkinter.colorchooser import *
import copy

# Function variables

space_width = 30 # Default value: 30
letter_height = 100 # Default value: 100
letter_width = 50 # Default value: 50



change = (input("Would you like to change the size of the letters from the defult value? y/any other input: "))

y = ("yes")
n = ("no")


if change in y:
    try:
        while True:
            try:
                letter_height = int(input("Enter a value from 1-170 to set the height of each letter in pixels: "))
                break
            except ValueError:
                print("That is not an integer! Please enter an integer from 1-170!")
        while letter_height > 170:
            try:
                letter_height = int(input("That value is too much. Please reenter a value from 1-170: "))
                while letter_height < 1:
                    letter_height = int(input("That value is too little. Please reenter a value from 1-170: "))
            except ValueError:
                print("That is not an integer! Please enter an integer from 1-170!")
        while letter_height < 1:
            try:
                letter_height = int(input("That value is too little. Please reenter a value from 1-170: "))
                while letter_height > 170:
                    letter_height = int(input("That value is too much. Please reenter a value from 1-170: "))
            except ValueError:
                print("That is not an integer! Please enter an integer from 1-170!")
        while True:
            try:
                letter_width = int(input("Enter a value from 1-170 to set the width of each letter in pixels: "))
                break
            except ValueError:
                print("That is not an integer! Please enter an integer from 1-170!")
        while letter_width > 170:
            try:
                letter_width = int(input("That value is too much. Please reenter a value from 1-170: "))
                while letter_width < 1:
                    letter_width = int(input("That value is too little. Please reenter a value from 1-170: "))
            except ValueError:
                print("That is not an integer! Please enter an integer from 1-170!")
        while letter_width < 1:
            try:
                letter_width = int(input("That value is too little. Please reenter a value from 1-170: "))
                while letter_width > 170:
                    letter_width = int(input("That value is too much. Please reenter a value from 1-170: "))
            except ValueError:
                print("That is not an integer! Please enter an integer from 1-170!")
            break
    except:
        input("That is not an answer! Please enter either y or n!: ")

elif change in n:
    space_width = 30 # Default value: 30
    letter_height = 100 # Default value: 100
    letter_width = 50 # Default value: 50

#The 'while' loop below will tell the user to choose a color name, but if the color is invalid, an exception is thrown, and the user must reenter a color name until a valid color name is entered. 

while True:
    try:
        pen_color=input("Enter a color name to set the pen color: ")
        pencolor(pen_color)
        break
    except:
        print("That is either not an available color or not a valid color name. Please reenter the name of another color or a valid one.")

#pen_width=input("Enter a number to set the pen width: ")
angle_height = (sqrt(letter_width**2 + (letter_height/2)**2))
a = (degrees(acos(letter_width/angle_height)))
b = (90-a)
R_angle = (90+b)

D_angle = (90+(degrees(acos(letter_width/angle_height))))
Second_D_angle = ((90 - (D_angle-90)) + (90-(degrees(acos(letter_width/angle_height)))))
D_space_angle = (degrees(atan(letter_width/(letter_height/2))))
space_angle = (180 - R_angle)

def NewLetterDimensions():
    def NewLetterHeight():
        letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
    def NewLetterWidth():
        letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
    NewLetterHeight()
    NewLetterWidth()
    listen()



def draw_space():
    # Add a space 30 pixels wide.
    penup()
    forward(space_width)
    pendown()

def move_turtle():
    # Pick up the turtle and move it to its starting location.
    penup()
    goto(-200, 100)
    pendown()

def draw_H():
    # Draw the left leg of H.
    # The turtle starts at the bottom left of the letter, pointing right.
    left(90)
    forward(letter_height)
    # Draw the bar of the H.
    # The turtle starts at the top of the left leg, pointing up.
    forward(-letter_height/2)
    right(90)
    forward(letter_width)
    # Draw the right leg of the H.
    # The turtle starts at the right side of the bar, pointing right.
    left(90)
    forward(letter_height/2)
    forward(-letter_height)
    right(90)
    # The H is drawn.
    # The turtle is in the top right, pointing right.
    draw_space()


def draw_E():
    # Draw an E.
    left(90)
    forward(letter_height)
    right(90)
    forward(letter_width)
    forward(-letter_width)
    right(90)
    forward(letter_height/2)
    left(90)
    forward(letter_width)
    forward(-letter_width)
    right(90)
    forward(letter_height/2)
    left(90)
    forward(letter_width)
    draw_space()

def draw_L():
    # Draw an L
    left(90)
    forward(letter_height)
    forward(-letter_height)
    right(90)
    forward(letter_width)
    draw_space()

def draw_O():
    # Draw an O
    forward(letter_width)
    left(90)
    forward(letter_height)
    left(90)
    forward(letter_width)
    left(90)
    forward(letter_height)
    left(90)
    forward(letter_width)
    draw_space()

def draw_newline():
    # This funtion will pick up the turtle and move it to a second line below HELLO
    penup()
    goto(xcor(), ycor() -letter_height-5)
    pendown()

def draw_W():
    # This function will draw a W
    left(105)
    forward(letter_height)
    backward(letter_height)
    right(40)
    forward(letter_height/2)
    right(131)
    forward(letter_height/2)
    left(141)
    forward(letter_height)
    right(165)
    penup()
    forward(letter_height)
    left(90)
    draw_space()

def draw_second_O():
    # This function will draw the O in "world"
    forward(letter_width)
    right(90)
    forward(letter_height)
    right(90)
    forward(letter_width)
    right(90)
    forward(letter_height)
    right(90)
    penup()
    forward(letter_width)
    right(90)
    forward(letter_height)
    left(90)
    draw_space()

def draw_R():
    # This function will draw an R
    left(90)
    forward(letter_height)
    right(90)
    forward(letter_width)
    right(90)
    forward(letter_height/2)
    right(90)
    forward(letter_width)
    left(R_angle)
    forward(angle_height)
    left(space_angle)
    draw_space()

def draw_D():
    # This function will draw a REAL D
    left(90)
    forward(letter_height)
    right(D_angle)
    forward(angle_height)
    right(Second_D_angle)
    forward(angle_height)
    left(90+D_space_angle)
    penup()
    forward(letter_width)
    draw_space()

def skip(x, y):
penup()
goto(x, y)
pendown()

def back():
    penup()
    bk(letter_width + space_width)
    pendown()

def walk():
    penup()
    forward(letter_width + space_width)
    pendown()

def soar():
    penup()
    left(90)
    forward(letter_height + 5)
    right(90)
    pendown()

def fall():
    penup()
    right(90)
    forward(letter_height + 5)
    left(90)
    pendown()

setup(1.0, 1.0)

def RotateRight():
    right(90)

def RotateLeft():
    left(90)

def Up():
    penup()
    goto(xcor(),ycor()+(letter_height+5))
    pendown()

def width1():
    width(1)

def width2():
    width(2)

def width3():
    width(3)

def width4():
    width(4)

def width5():
    width(5)

def width6():
    width(6)

def width7():
    width(7)

def width8():
    width(8)

def width9():
    width(9)

def width10():
    width(10)

def Blue():
    color("blue")

def Red():
    color("red")

def DarkGreen():
    color("dark green")

def Purple():
    color("purple")

def Pink():
    color("pink")

def Brown():
    color("brown")

def Orange():
    color("orange")

def Black():
    color("Black")

def OriginalColor():
    color(pen_color)

def getColor():
Color = askcolor()
color_name = Color[1]
colormode(255)
color(color_name)

move_turtle()
speed(0)
color(pen_color)
listen()
onkey(NewLetterDimensions, "k")
onkey(width1, "1")
onkey(width2, "2")
onkey(width3, "3")
onkey(width4, "4")
onkey(width5, "5")
onkey(width6, "6")
onkey(width7, "7")
onkey(width8, "8")
onkey(width9, "9")
onkey(width10, "0")
onkey(Blue, "F1")
onkey(Red, "F2")
onkey(DarkGreen, "F3")
onkey(Purple, "F4")
onkey(Pink, "F5")
onkey(Brown, "F6")
onkey(Orange, "F7")
onkey(Black, "F8")
onkey(getColor, "F9")
onkey(OriginalColor, "F10")
onscreenclick(goto)
onscreenclick(skip)
onkey(clear, "space")
onkey(back, "BackSpace")
onkey(walk, "Tab")
onkey(Up, "Up")
onkey(draw_H, "h")
onkey(bye, "Escape")
onkey(draw_E, "e")
onkey(draw_L, "l")
onkey(draw_O, "o")
onkey(draw_W, "w")
onkey(draw_R, "r")
onkey(draw_D, "d")
onkey(draw_newline, "Return")

现在,问题在于,当用户按下字母“K”时出现对话框,并且用户输入相应的宽度和高度值时,字母的尺寸执行 更改!我在这做错了什么?任何帮助都非常感谢! :)

1 个答案:

答案 0 :(得分:1)

这看起来只是一个范围问题!您应该能够轻松解决此问题,方法是将global letter_height添加到NewLetterHeight函数的顶部,将global letter_width添加到NewLetterWidth函数的顶部:

def NewLetterDimensions():
def NewLetterHeight():
    global letter_height
    letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
def NewLetterWidth():
    global letter_width
    letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
NewLetterHeight()
NewLetterWidth()
listen()