0

I am making a game and the first thing on the screen is a button that says play game. But the button isn't showing up on the screen for some reason? The function play_sound_game is basically the rest of my code.

I have already tried removing turtle.mainloop() but that doesn't work either.

    import turtle
    import tkinter as tk
    import time
    import pygame

    screen = turtle.Screen()

    turtle.ht()
    screen.bgcolor("blue")
    turtle.color('deep pink')
    style = ('Courier', 80, 'italic')
    turtle.pu()
    turtle.goto(-318,176)
    turtle.pu
    turtle.write('RHYMING WORDS', font=style)
    turtle.hideturtle()


    turtle.mainloop()


    #Button for play game
    button_playgame = tk.Button(canvas.master, text="Play Game", command=play_sound_game, font=('Arial', '65',"bold"), foreground = 'red')

    button_playgame.config(height = -1, width = 4)
    canvas.create_window(272, 88, window=button_playgame)

I didn't get any error messages.

Nouman
  • 6,947
  • 7
  • 32
  • 60
Python123
  • 35
  • 1
  • 6

1 Answers1

1

turtle uses widget Canvas from module tkinter. To add button you have to get access to this canvas

canvas = screen.getcanvas()

and then you can use it in

tk.Button(canvas.master, ...)

and

canvas.create_window(...)

Because turtle.mainloop() runs all time till you close window so you have to create button before mainloop()

Working example.

import turtle
import tkinter as tk

def play_sound_game():
    pass

screen = turtle.Screen()

turtle.ht()
screen.bgcolor("blue")
turtle.color('deep pink')
style = ('Courier', 80, 'italic')
turtle.pu()
turtle.goto(-318,176)
turtle.pu
turtle.write('RHYMING WORDS', font=style)
turtle.hideturtle()

canvas = screen.getcanvas()

button_playgame = tk.Button(canvas.master, text="Play Game", command=play_sound_game, font=('Arial', '65',"bold"), foreground='red')
#button_playgame.config(height=1, width=4)

canvas.create_window(272, 88, window=button_playgame)

turtle.mainloop()

On Linux

enter image description here

furas
  • 134,197
  • 12
  • 106
  • 148
  • Also one more question. Is it possible to change the background color of the button? – Python123 Aug 04 '19 at 22:05
  • yes, `background="blue"` or shorter `bg="blue"`. Using hex code (#RRGGBB) `bg="#FF00DD"`. Doc: [Button](http://effbot.org/tkinterbook/button.htm) – furas Aug 04 '19 at 22:19
  • The button color stays at white – Python123 Aug 04 '19 at 22:41
  • on Linux it changes color to blue. It is white only when mouse is over button (hover) - but button has other options to change it. If you use other system then Linux then maybe your system not let change it. – furas Aug 04 '19 at 23:00
  • Yeah I am using mac so probably – Python123 Aug 05 '19 at 23:14