简体   繁体   English

导入绘图功能时,禁止Tkinter打开第二个窗口

[英]Stop Tkinter from opening a second window when importing drawing function

I am having an issue that I am having trouble resolving. 我遇到无法解决的问题。 I am making a Python interface with Tkinter that allows the user to draw a predefined figure with some parameters (number and length). 我正在用Tkinter制作一个Python界面,允许用户绘制带有一些参数(数字和长度)的预定义图形。 The predefined figure function "tree" is in a second python file. 预定义的图形函数“树”位于第二个python文件中。 The app runs fine if the "tree" function is in the main python file ie everything draws in one window. 如果“树”功能位于主python文件中,即所有内容都绘制在一个窗口中,则该应用运行良好。 If I put the figure "tree" in a second python file (figures.py) and try to import it the app will create a second window and the tree figure will draw there instead of the intended main window. 如果我将图“树”放在第二个python文件(figures.py)中并尝试导入,则应用程序将创建第二个窗口,树图将绘制在此处而不是预期的主窗口。 How can I reference and import the function so that it draws in the main app window. 如何引用和导入函数,以便在主应用程序窗口中绘制该函数。 Thanks! 谢谢!

main python file 主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 for storing predefined designs 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)

The app runs fine if the "tree" function is in the main python file ie everything draws in one window. 如果“树”功能位于主python文件中,即所有内容都绘制在一个窗口中,则该应用运行良好。 If I put the figure "tree" in a second python file (figures.py) and try to import it the app will create a second window and the tree figure will draw there instead of the intended main window. 如果我将图“树”放在第二个python文件(figures.py)中并尝试导入,则应用程序将创建第二个窗口,树图将绘制在此处而不是预期的主窗口。

The problem is that figures.py is setting up a turtle environment independent of the main program -- don't let it. 问题在于,Figures.py正在设置独立于主程序的乌龟环境-别让它。 Pass in to the figures.py functions whatever they need to operate in the main program's turtle environment: 传递给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()

The code at the bottom of the file is so you can test this file independently. 文件底部的代码是因此您可以独立测试此文件。 When imported into the main program, it will be ignored. 当导入主程序时,它将被忽略。 Now we just need a small change to the main program: 现在我们只需要对主程序进行一点改动:

main python file 主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)

I changed the variable names as you were redefining Python's built-in len function. 当您重新定义Python的内置len函数时,我更改了变量名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM