我是一名刚开始学习Python编程的11年级学生(5年级学生)。我正在尝试制作一个GUI,我可以选择我想要用乌龟绘制的形状并输入绘制形状所需的值。我设法使用以下代码(我已经完成了龟分形的功能并导入了该文件):
from tkinter import *
import turtlegraphics
from turtle import *
import math
#set up the window
root = Tk()
root.title('Turtle shapes')
root.geometry('300x200+100+100')
#make the interface
turtleLabel = Label(root, text = 'Turtle Fractals Generator')
turtleLabel.grid(row = 0, column =1, columnspan =2)
#order widgets
orderLabel = Label(root, text ='Order')
orderLabel.grid(row = 1, column =0)
orderStr = StringVar()
orderEntry = Entry(root, textvariable = orderStr)
orderEntry.grid(row = 1, column = 1, columnspan = 2)
#length widgets
lengthLabel = Label(root, text ='Length')
lengthLabel.grid(row = 2, column =0)
lengthStr = StringVar()
lengthEntry = Entry(root, textvariable = lengthStr)
lengthEntry.grid(row = 2, column = 1, columnspan = 2)
#button widgets
def clearF():
#Clear the entries
orderStr.set('')
lengthStr.set('')
return
#end def
clearButton = Button(root, text ='Clear', command = clearF)
clearButton.grid(row = 3, column =1)
def treeF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.tree(order,length, pen)
return
#end def
treeButton = Button(root, text ='Tree', command = treeF)
treeButton.grid(row = 1, column =3)
#make a gasket button
def gasketF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.gasket(order,length,pen)
return
#end def
gasButton = Button(root, text ='Gasket', command = gasketF)
gasButton.grid(row = 2, column =3)
#make a dandelion button
def dandelionF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.tree4(order,length,pen)
return
#end def
danButton = Button(root, text ='Dandelion', command = dandelionF)
danButton.grid(row = 3, column =3)
#make flalke button
def kevinF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.k(order,length,pen)
return
#end def
def flakeF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.flake(order,length,pen)
return
#end def
flakeButton = Button(root, text ='Flake', command = flakeF)
flakeButton.grid(row = 4, column =3)
#make a screen and a pen
pen = Pen()
screen = Screen()
pen.speed(0)
pen.color('blue')
pen.width(3)
screen.bgcolor('white')
root.mainloop()
但是,我想尝试列出一个列表来选择形状,而不是有几个按钮。我对列表了解不多,因为我还没有教过它,但在做完研究后这就是我得到的:
'''
program gui to draw turtle fractals
'''
from tkinter import *
import turtlegraphics
from turtle import *
import math
#make a screen and a pen
pen = Pen()
def treeF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.tree(order,length, pen)
return
#end def
#make a gasket button
def gasketF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.gasket(order,length,pen)
return
#end def
#make a dandelion button
def dandelionF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.tree4(order,length,pen)
return
#end def
#make flalke button
def kevinF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.k(order,length,pen)
return
#end def
def flakeF():
order = int(orderStr.get())
length = float(lengthStr.get())
turtlegraphics.flake(order,length,pen)
return
#end def
screen = Screen()
pen.speed(0)
pen.color('blue')
pen.width(3)
screen.bgcolor('white')
#set up the window
root = Tk()
root.title('turtle shapes')
root.geometry('600x300+100+100')
#make the interface
turtleLabel = Label(root, text = 'Turtle Fractals Generator')
turtleLabel.grid(row = 0, column =1, columnspan =2)
#order widgets
orderLabel = Label(root, text ='Order')
orderLabel.grid(row = 1, column =0)
orderStr = StringVar()
orderEntry = Entry(root, textvariable = orderStr)
orderEntry.grid(row = 1, column = 1, columnspan = 2)
#length widgets
lengthLabel = Label(root, text ='Length')
lengthLabel.grid(row = 2, column =0)
lengthStr = StringVar()
lengthEntry = Entry(root, textvariable = lengthStr)
lengthEntry.grid(row = 2, column = 1, columnspan = 2)
#button widgets
def clearF():
#Clear the entries
orderStr.set('')
lengthStr.set('')
return
#end def
clearButton = Button(root, text ='Clear', command = clearF)
clearButton.grid(row = 3, column =1)
#list
def selection():
obj = nameList.curselection()[0]
if obj == 'tree':
command = treeF
return
if obj == 'dandelion':
command = dandelionF
return
if obj == 'flake':
command = flakeF
return
if obj == 'gasketF':
command = gasketF
return
#end if
#end def
#create list
listbox = Listbox(root,selectmode = SINGLE) #select mode is type of
selection
listButton= Button(root, text = 'chose fractal')
labelVar = StringVar()
nameLabel = Label(root, textvariable = labelVar)
nameLabel.grid(row = 2 , column = 7, columnspan = 2)
labelVar.set('choose a fractal')
names = ['tree','dandelion','flake','gasket']
nameList = Listbox(root)
for i in names:
nameList.insert(END, i)
nameList.grid(row = 4, column =7, columnspan = 2)
selectionButton = Button(root, text = 'draw', command = selection)
selectionButton.grid(row = 5, column = 7, columnspan = 2)
root.mainloop()
我知道我的代码有问题,但我不知道我做错了什么。当我运行我的代码时,我没有得到任何错误,但它没有绘制形状。此外,单击时清除按钮不起作用。我怎么能解决这个问题?
答案 0 :(得分:0)
您的直接问题是selection()
如何工作 - 它将数字与字符串进行比较,并拥有执行任何子程序所需的代码。我在下面做了一些改编,以获得你想要的行为。我还做了其他改动和简化:
from tkinter import *
from turtle import Pen, Screen
import turtlegraphics
def treeF():
order = int(orderEntry.get())
length = float(lengthEntry.get())
turtlegraphics.tree(order, length, pen)
def dandelionF():
order = int(orderEntry.get())
length = float(lengthEntry.get())
turtlegraphics.tree4(order, length, pen)
def flakeF():
order = int(orderEntry.get())
length = float(lengthEntry.get())
turtlegraphics.flake(order, length, pen)
def gasketF():
order = int(orderEntry.get())
length = float(lengthEntry.get())
turtlegraphics.gasket(order, length, pen)
def kevinF():
order = int(orderEntry.get())
length = float(lengthEntry.get())
turtlegraphics.k(order, length, pen)
fractals = [('tree', treeF), ('dandelion', dandelionF), ('flake', flakeF), ('gasket', gasketF), ('kevin', kevinF)]
# make a screen and a pen
screen = Screen()
screen.bgcolor('white')
pen = Pen()
pen.speed('fastest')
pen.color('blue')
pen.width(3)
# set up the window
root = Tk()
root.title('turtle shapes')
root.geometry('600x400+100+100')
# make the interface
turtleLabel = Label(root, text='Turtle Fractals Generator')
turtleLabel.grid(row=0, column=1, columnspan=2)
# order widgets
orderLabel = Label(root, text='Order')
orderLabel.grid(row=1, column=0)
orderEntry = Entry(root)
orderEntry.grid(row=1, column=1, columnspan=2)
# length widgets
lengthLabel = Label(root, text='Length')
lengthLabel.grid(row=2, column=0)
lengthEntry = Entry(root)
lengthEntry.grid(row=2, column=1, columnspan=2)
# button widgets
def clearF():
# Clear the entries
orderEntry.delete(0, 'end')
lengthEntry.delete(0, 'end')
clearButton = Button(root, text='Clear', command=clearF)
clearButton.grid(row=3, column=1)
# list
def selection():
selections = nameList.curselection()
if selections:
selection = selections[0]
if selection < len(fractals):
name, definition = fractals[selection]
definition()
# create list
listbox = Listbox(root, selectmode=SINGLE) # select mode is type of selection
listButton = Button(root, text='chose fractal')
labelVar = StringVar()
nameLabel = Label(root, textvariable=labelVar)
nameLabel.grid(row=2, column=7, columnspan=2)
labelVar.set('choose a fractal')
nameList = Listbox(root)
for name, definition in fractals:
nameList.insert(END, name)
nameList.grid(row=4, column=7, columnspan=2)
selectionButton = Button(root, text='Draw', command=selection)
selectionButton.grid(row=5, column=7, columnspan=2)
root.mainloop()
关于您的代码的一般评论:
找到并遵循良好的Python编码风格 - 当您发明自己的编码“风格”时,它会使您更难理解代码。
这不是将turtle与tkinter集成的正确方法。搜索StackOverflow或turtle文档,用于RawTurtle
和TurtleScreen
,您使用的类代替Turtle
(又名Pen
)和Screen
嵌入到现有的Tk窗口结构。