简体   繁体   中英

How to combine tkinter and turtle?

I have a part of codes like this

import turtle
turtle.bgcolor("green")
draw = turtle.Turtle()
draw.speed(1000000)
draw.hideturtle()
draw.pensize(3)
draw.color("white")

def Board (a, x, y, size):
    draw.pu()
    draw.goto(x, y)
    draw.pd()
    for i in range (0, 4):
        draw.forward(size)
        draw.right(90)

x =-40
y = -40
size = 40
for i in range (0, 10):
    for j in range (0, 10):
         Board (draw, x + j*size, y + i*size, size)

turtle.done()

And like this

import tkinter
import tkinter.messagebox
window = tkinter.Tk()
def Button_click ():
   tkinter.messagebox.showinfo("Game", "Tic Tac Toe")

button = tkinter.Button(window, text = "Play!", command = Button_click)
button.pack()
window.mainloop()

Since I'm trying to create a window with a button to enter the TicTacToe game (I haven't finished the rest, just only the board). Is there any way that I can do to combine both turtle and tkinter? Thank you

Yes. Python turtle operates in two modes, standalone and embedded in a larger tkinter program. Instead of Turtle and Screen , when using turtle embedded, you work with RawTurtle , TurtleScreen and optionally ScrolledCanvas . You build your tkinter interface as needed, using a Canvas to contain your turtle graphics. You can find examples of embedding turtle in tkinter by searching SO for RawTurtle .

Here's an example of the same code written both embedded and standalone .

I was playing with your code while cdlane was answering your question! As cdlane said I replaced "turtle" with "RawTurtle" and put your button on the same window as the canvas. I prefer using grid than pack when placing things because I feel like I have more control.

import tkinter
import turtle
import tkinter.messagebox

window = tkinter.Tk()

canvas = tkinter.Canvas(master = window, width = 800, height = 800)
canvas.grid(padx=2, pady=2, row=0, column=0, rowspan=10, columnspan=10) # , sticky='nsew')
#draw = turtle.Turtle()
draw = turtle.RawTurtle(canvas)

def Board(a, x, y, size):
    #draw.pu()
    draw.penup()
    draw.goto(x,y)
    #draw.pd()
    draw.pendown()
    for i in range (0, 4):
        draw.forward(size)
        draw.right(90)

def Board2():
    x =-40
    y = -40
    size = 40
    for i in range (0, 10):
        for j in range (0, 10):
            Board(draw, x + j*size, y + i*size, size)

def Button_click ():
    tkinter.messagebox.showinfo("Game", "Tic Tac Toe")

#button = tkinter.Button(window, text = "Play!", command = Button_click)
#button = Tk.Button(window, text = "Play!", command = Button_click)
#button.pack()
#
Play_Button = tkinter.Button(master = window, text ="Play!", command = Button_click)
Play_Button.config(bg="cyan",fg="black")
Play_Button.grid(padx=2, pady=2, row=0, column=11, sticky='nsew')

Board_Button = tkinter.Button(master = window, text ="Draw_Board", command = Board2)
Board_Button.config(bg="cyan",fg="black")
Board_Button.grid(padx=2, pady=2, row=1, column=11, sticky='nsew')
#
window.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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