我遇到了无法解决的问题。我正在用Tkinter制作一个Python界面,允许用户绘制带有一些参数(数字和长度)的预定义图形。预定义的图形函数“树”位于第二个python文件中。如果“树”功能位于主python文件中,即所有内容都绘制在一个窗口中,则该应用可以正常运行。如果我将图“树”放在第二个python文件(figures.py)中并尝试导入,则应用程序将创建第二个窗口,树图将绘制在此处而不是预期的主窗口。如何引用和导入函数,以便在主应用程序窗口中绘制该函数。谢谢!
主要python文件
import turtle
import tkinter
from tkinter.ttk import *
import figures
# Main function is defined.
def main():
# Set root and create canvas
root = tkinter.Tk()
root.title("Draw")
canvas = tkinter.Canvas(root, width=800, height=700)
canvas.pack(side=tkinter.RIGHT)
# create a turtle to draw on the canvas
pen = turtle.RawTurtle(canvas)
screen = pen.getscreen()
# Set screen co-ordinates.
screen.setworldcoordinates(-200, -700, 800, 700)
screen.bgcolor("grey")
# Draw frame
frame = tkinter.Frame(root, bg="white")
frame.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
pointLabel = tkinter.Label(frame, text="Fractal", bg="white", )
pointLabel.pack()
# make the dropdown for fractal list
turtleNames = ["Tree", "Dandelion"]
turtleStr = tkinter.StringVar()
turtleList = OptionMenu(frame, turtleStr, turtleNames[0], *turtleNames)
turtleList.pack()
numberLabel = tkinter.Label(frame, text="Number")
numberLabel.pack()
# the entry widget must be given a string.
number = tkinter.StringVar()
numberEntry = tkinter.Entry(frame, textvariable=number)
numberEntry.pack()
number.set(str(3))
lengthLabel = tkinter.Label(frame, text="Length")
lengthLabel.pack()
# User sets length
length = tkinter.StringVar()
lengthEntry = tkinter.Entry(frame, textvariable=length)
lengthEntry.pack()
length.set(str(200))
def drawHandler():
# get the value from orderStr and make int
num = int(number.get())
# get the value from lengthStr and make int
len = int(length.get())
figures.tree(num, len)
# Event handler to clear canvas for a new drawing
def clearHandler():
pen.clear()
# This is an event handler. Handling the quit button press results in quitting the application.
def quitHandler():
root.destroy()
root.quit()
# Draw Buttons
# presses of the "Draw" button.
drawButton = tkinter.Button(frame, text="Draw", command=drawHandler)
drawButton.pack()
# presses of the "Clear" button.
clearButton = tkinter.Button(frame, text="Clear", command=clearHandler)
clearButton.pack()
# presses of the "Quit" button.
quitButton = tkinter.Button(frame, text="Quit", command=quitHandler)
quitButton.pack()
# tells the application to enter its event processing loop
tkinter.mainloop()
# Python jumps right here after executing the def main() line. These two lines tell
if __name__ == "__main__":
main()
figures.py用于存储预定义的设计
from turtle import *
pen = Pen()
screen = Screen()
# 1st figure Tree
def tree(n, l):
if n == 0 or l < 2:
return
# endif
pen.forward(l)
pen.left(45)
tree(n - 1, l / 2)
pen.right(90)
tree(n - 1, l / 2)
pen.left(45)
pen.backward(l)
答案 0 :(得分:1)
如果“树”功能位于主python文件中,则该应用运行良好 即一切都在一个窗口中绘制。如果我把图“树”放在一个 第二个python文件(figures.py)并尝试将其导入,应用将 创建第二个窗口,树形图将在那里绘制,而不是 预期的主窗口。
问题在于,Figures.py正在建立独立于主程序的乌龟环境-别让它。传递给Figures.py函数,使其在主程序的乌龟环境中进行操作所需的一切:
figures.py
# 1st figure Tree
def tree(pen, number, length):
if number == 0 or length < 2:
return
pen.forward(length)
pen.left(45)
tree(pen, number - 1, length / 2)
pen.right(90)
tree(pen, number - 1, length / 2)
pen.left(45)
pen.backward(length)
# test this code standalone
if __name__ == "__main__":
import turtle
tree(turtle.getpen(), 5, 100) # test using default turtle
turtle.exitonclick()
该文件底部的代码是因此您可以独立测试该文件。当导入主程序时,它将被忽略。现在,我们只需要对主程序进行少量更改:
主要python文件
def drawHandler():
# get the value from orderStr and make int
number_int = int(number.get())
# get the value from lengthStr and make int
length_int = int(length.get())
figures.tree(pen, number_int, length_int)
当您重新定义Python的内置len
函数时,我更改了变量名。