1105748041 发表于 2022-10-30 19:57:05

请问python怎么赋值

请问大家怎么把pen speed和pen width赋值给画笔以及实现hide turtle和hide trace(跳过画图过程)功能,图片和代码如下:
from tkinter import *
from tkinter.ttk import *
from turtle import *
import turtlefigures
from tkinter import colorchooser

# create an empty frame and setup its geometry
root = Tk()
root.geometry("800x500+200+200")
root.title("Turtle Interface")

# create the canvas
canvasFrame = LabelFrame(root, text = "Turtle Canvas")
canvas = Canvas(canvasFrame, width = 400, height = 400)
canvas.pack()
canvasFrame.grid(row = 0, column = 4, rowspan = 3, columnspan = 3,
               padx = 10, pady = 10)

# make the screen
screen = TurtleScreen(canvas)
screen.bgcolor("white")
w, h = screen.screensize()


# make the pen
pen = RawTurtle(screen)
pen.color("red")
pen.width(3)

# make a control panel and grid it
controlFrame = LabelFrame(root, text = "Control Panel")
controlFrame.grid(row = 0, column = 0, columnspan = 3, padx = 10)
      
def getColor():
    color_code = colorchooser.askcolor()
colorButton = Button(controlFrame, text = "Pen Color Picker", command = getColor)
colorButton.grid(row = 0, column = 0, columnspan = 3,pady=10,ipadx=50,ipady=10)

#create speed scale
def penSpeed(currentSpeed):
    currentSpeed = penspeedStr.get()
    pen.speed(currentSpeed)

penspeedStr=DoubleVar()
scale = Scale(controlFrame,orient=HORIZONTAL,length=150,from_=0.0,to=10.0,
            variable=penSpeed,command=penSpeed)
scale.bind('<ButtonRelease-1>',penSpeed)
scale.grid(row = 1, column = 1, columnspan = 3)
penspeedLabel = Label(controlFrame,text='Pen Speed')
penspeedLabel.grid(row = 1, column = 0,pady=10)



#create size scale
def penSize(currentSize):
    currentSize = pensizeStr.get()
    pen.size(currentSize)
pensizeStr=DoubleVar()
scale = Scale(controlFrame,orient=HORIZONTAL,length=150,from_=1.0,to=10.0,
            command=penSize)
scale.bind('<ButtonRelease-1>',penSize)
scale.grid(row = 2, column = 1, columnspan = 3)
pensizeLabel = Label(controlFrame,text='Pen Size')
pensizeLabel.grid(row = 2, column = 0,pady=10)


def turtleshape():
   if(CheckVar1.get()==0 and CheckVar2.get()==0 ):
         s = ''
   else:
         s1 = "Hide Turtle" if CheckVar1.get()==1 else ""
         s2 = "Hide Trace" if CheckVar2.get() == 1 else ""
CheckVar1 = IntVar()
CheckVar2 = IntVar()
ch1 = Checkbutton(controlFrame,text='Hide Turtle',variable = CheckVar1,onvalue=1,offvalue=0)
ch2 = Checkbutton(controlFrame,text='Hide Trace',variable = CheckVar2,onvalue=1,offvalue=0)
ch1.grid(row = 3, column = 0)
ch2.grid(row = 4, column = 0)
TurtleButton = Button(controlFrame, text = "Turtle Control")
TurtleButton.grid(row = 5,column = 0,columnspan = 2,padx=20,
                  pady=10,ipadx=50,ipady=10)



orderLabel = Label(controlFrame, text = "Order")
orderLabel.grid(row = 7, column = 0)

orderStr = StringVar()
orderEntry = Entry(controlFrame, textvariable = orderStr)
orderEntry.grid(row = 7, column = 1, columnspan = 2)

lengthLabel = Label(controlFrame, text = "Length")
lengthLabel.grid(row = 8, column = 0)

lengthStr = StringVar()
lengthEntry = Entry(controlFrame, textvariable = lengthStr)
lengthEntry.grid(row = 8, column = 1, columnspan = 2)



def onClearF():
       # clear the entries
       orderStr.set("")
       lengthStr.set("")

       # also clear the screen
       screen.resetscreen()

       # reset dropdown
       figureStr.set("Binary Tree")

#end onClear
clearButton = Button(controlFrame, text = "Clear", command = onClearF)
clearButton.grid(row = 10, column = 0,ipadx=40,ipady=10)

# make the OptionMenu dropdown list
figureList = ["Binary Tree", "Dandelian", "Fern", "Koch", "Anti-Flake", "Flake","Gasket", "Windmill"
            , "Ccurve", "Pyramid", "Rectangle","maze"]
figureStr = StringVar()
figureOptionMenu = OptionMenu(controlFrame, figureStr, figureList, *figureList)
figureOptionMenu.grid(row = 9, column = 0,columnspan = 2,padx=20,pady=10,ipadx=50,ipady=10)

def onDrawF():
    # get the order and length
    order = int(orderStr.get())
    length = int(lengthStr.get())

    #what is the figure to draw
    figure = figureStr.get()
    figureId = figureList.index(figure)

    # check what to draw
    if figureId == 0:# draw the binary tree
      pen.up()
      pen.backward(w/2)
      pen.down()
      turtlefigures.tree(order, length, pen)

    elif figureId == 1:# draw the dandelion
      pen.up()
      pen.backward(w/2)
      pen.down()
      turtlefigures.tree4(order, length, pen)

    elif figureId == 2:# draw the fern
      pen.up()
      pen.backward(w/2)
      pen.down()
      turtlefigures.fern(order, length, pen)
    elif figureId == 3:
      pen.up()
      pen.backward(w/4)
      pen.down()
      turtlefigures.koch(order, length, pen)
    elif figureId == 4:
      pen.up()
      pen.backward(w/4)
      pen.down()
      turtlefigures.flake(order, length, pen)
    elif figureId == 5:
      pen.up()
      pen.backward(w/4)
      pen.down()
      turtlefigures.flake1(order, length, pen)
    elif figureId == 6:
      turtlefigures.gasket(order, length, pen)
    elif figureId == 7:
      turtlefigures.carpet(order, length, pen)
    elif figureId == 8:
      turtlefigures.ccurve(order, length, pen)
    elif figureId == 9:
      pen.up()
      pen.backward(w/4)
      pen.down()
      turtlefigures.cc(order, length, pen)
    elif figureId == 10:
      pen.up()
      pen.backward(w/4)
      pen.down()
      turtlefigures.ccc(order, length, pen)
    elif figureId == 11:
      pen.up()
      pen.backward(w/4)
      pen.down()
      turtlefigures.maze(order, length, pen)

# end of draw


#end onConvertF      
drawButton = Button(controlFrame, text = "Draw", command = onDrawF)
drawButton.grid(row = 10, column = 1, ipadx=40,ipady=10)

root.tk.call("source", "azure.tcl")
root.tk.call("set_theme", "dark")






Brick_Porter 发表于 2022-10-30 19:57:06

1105748041 发表于 2022-10-31 18:25
谢谢,我已经全部解决啦。现在有个新的问题,如果我想添加一个描述性文本框,根据选择figure的不同给不同 ...

你在界面上增加一个新的label,就是tkinter中的label标签,根据选择的不同显示不同的内容就行了呀
假设label组件的名字叫做lb,那么你可以这样写lb['txt'] = 选择的内容

Brick_Porter 发表于 2022-10-30 21:37:45

使用你定义的CheckVar1和CheckVar2这两个变量就行了。

在onDrawF这个函数中添加两个分支语句,一个分支检查CheckVar1,如果它的值是1就隐藏turtle,否则就显示turtle;另一个分支检查CheckVar2,如果它的值是1就隐藏绘制轨迹,否则就正常显示轨迹

1105748041 发表于 2022-10-31 18:25:03

Brick_Porter 发表于 2022-10-30 21:37
使用你定义的CheckVar1和CheckVar2这两个变量就行了。

在onDrawF这个函数中添加两个分支语句,一个分支 ...

谢谢,我已经全部解决啦。现在有个新的问题,如果我想添加一个描述性文本框,根据选择figure的不同给不同的文本框该怎么做呢
页: [1]
查看完整版本: 请问python怎么赋值